Prashanth said:
Hi ,
I'm Prashanth Badabagni .. I have and idea how a program prints
it's own source code ..
Look up 'Quine' using Google. Fortunately for you, this has been done to
death already and is well represented on the web. There is a handful of
people who find this an interesting problem after first encounters with it;
for the rest of us it is trite and tiresome. Your code below has some
points of interest. They suggest, among other things, that you are
unfamiliar with customary behavior in newsgroups. Just so you know, before
posting to a newsgroup you should
(1) follow the newsgroup for a while to find out what questions are topical
and to be sure that you don't make any obvious errors which are
regularly attacked with vigor.
(2) look for the FAQ. Most major newsgroups have one. When you find it,
check to see if your question has been answered. Our FAQ can be found
at <
http://www.eskimo.com/~scs/C-faq/top.html>. In this case, your
question is realted to "20.34 How do you write a program which produces
its own source code as its output?" which is found at
<
http://www.eskimo.com/~scs/C-faq/q20.34.html>.
Now, let's take a look at the program (I'm sure to miss some things):
^^^^
This will set the alarm bells ringing. main *always* returns an int in a
hosted implementation. Even Microsoft has gotten around to fixing most of
its illiterate help files to reflect this.
^^^^
which you need for FILE said:
char *file,c;
strcpy(file,__FILE__);
^^^^^^
You have not #included <string.h>, which you want for strcpy.
Since you have not allocated any space for file to point to, and file is
pointing anywhere, any attempt to strcpy to this unallocated, unknown space
will probably cause a fault (if you are lucky).
Since you need to call malloc, or one of its relatives, to allocate this
space said:
^^
You declared P as the FILE*, and here you use p. C is case-sensitive, so p
has never been declared.
if(!p)
{
printf("error opening the file .....");
If you do not terminate the final line with a line-terminating character
('\n'), there is no portable way of knowing what will happen. Also,
consider reporting such errors via fprintf(stderr,...
When there are no parameters beyond the text message, you might also
consider using puts or fputs instead.
It is not an error to return 0 here, but there *has* been an error causing
termination. You might want to say so. You do this with