How can I write code to show itself

1.1k views Asked by At

How can I write a code to show itself (print code to console) using standard C++ only without any external library?

2

There are 2 answers

3
Chris Eberle On BEST ANSWER

Tada: http://en.wikipedia.org/wiki/Quine_(computing)

On a slightly more pragmatic note, almost no one ever does this. It's pointless. If you want to distribute source code, just put it into a tarball or zip file like a sane person.

3
Fatih Donmez On
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main () {
  string line;
  ifstream sourceFile(__FILE__);
  if (sourceFile.is_open())
  {
    while ( sourceFile.good() )
    {
      getline (sourceFile,line);
      cout << line << endl;
    }
    sourceFile.close();
  }

  else cout << "Unable to open source file"; 

  return 0;
}