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

有帮助吗?

解决方案

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.

其他提示

#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;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top