Hi, I have seen a program in a thread that write a program which displays its own source code...
How this can be done??

Recommended Answers

All 3 Replies

Its stupid :

#include<iostream>

using std::cout;

int main()
{
   int a = 0;
   cout<<"#include<iostream>\n\nusing\tstd::cout;\n\nint main()\n{\n\tint\ta = 0;\n\n\n\treturn 0;\n}\n\n";

return 0;
}

here is the basic program which can print itself

#include<iostream>

int main ()
{
  int c;
  FILE *f = fopen (__FILE__, "r");

  if (!f)
    return 1;

  for (c=fgetc(f); c!=EOF; c=fgetc(f))
    putchar (c);

  fclose (f);
  return 0;
}

Well at first place i didnt want to give the souce code , but i guess its the only way for u to understand on how it works and incorporate that in what ever ur doing .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.