470,863 Members | 1,083 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,863 developers. It's quick & easy.

Output as SOURCE CODE itself !!

Dev
Hello Folks,

I need a "C" program, whose output display its SOURCE CODE
itself..

How can we do that? Please post solutions along with code
example (if possible)

Thanx..

Feb 9 '06 #1
13 2116
Dev said:
Hello Folks,

I need a "C" program, whose output display its SOURCE CODE
itself..

How can we do that? Please post solutions along with code
example (if possible)


/* save this file as foo.c */
#include <stdio.h>

int main(void)
{
FILE *fp = fopen("foo.c", "r");
if(fp != NULL)
{
int ch;
while((ch = getc(fp)) != EOF)
{
putchar(ch);
}
fclose(fp);
}
return 0;
}

Alternatively, look up "quine" on the Web.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Feb 9 '06 #2

Richard Heathfield wrote:
Dev said:
Hello Folks,

I need a "C" program, whose output display its SOURCE CODE
itself..

How can we do that? Please post solutions along with code
example (if possible)
/* save this file as foo.c */
#include <stdio.h>

int main(void)
{
FILE *fp = fopen("foo.c", "r");

---------------------------------------------------------------------------------
replace the above line with
FILE *fp = fopen(__FILE__,"r");
--------------------------------------------------------------------------------- if(fp != NULL)
{
int ch;
while((ch = getc(fp)) != EOF)
{
putchar(ch);
}
fclose(fp);
}
return 0;
}

Alternatively, look up "quine" on the Web.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


Feb 9 '06 #3
Dev
Thanks Richard, that's what i thought, with file handlings we can do
this..

Feb 9 '06 #4
"Dev" <kr**********@gmail.com> writes:
Thanks Richard, that's what i thought, with file handlings we can do
this..


One more time, please read <http://cfaj.freeshell.org/google/>
*before* you post here again. Thanks.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 9 '06 #5
Dev

Keith Thompson wrote:
"Dev" <kr**********@gmail.com> writes:
Thanks Richard, that's what i thought, with file handlings we can do
this..
One more time, please read <http://cfaj.freeshell.org/google/>
*before* you post here again. Thanks.


Hello Keith,
Thank for giving me link, i will follow the "Reply" format, sorry for
previous post, thanks once again..
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


Feb 9 '06 #6
Sunil Varma said:

Richard Heathfield wrote:

/* save this file as foo.c */
#include <stdio.h>

int main(void)
{
FILE *fp = fopen("foo.c", "r");

--------------------------------------------------------------------------------- replace the above line with
FILE *fp = fopen(__FILE__,"r");


Each has advantages and disadvantages.

__FILE__ will typically be replaced by a full path spec for the file, which
means you can run the program from anywhere on that system and still have
it pick up the file from that path - but if you move the program to a
different but compatible system, you must then reconstruct that path on the
new system and place the source file there, whereas my way you need only
dump the source file in any old place and then run the program from that
directory. (Yes, all these references to paths and directories make this
moderately off-topic.)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Feb 9 '06 #7
Dev said:
Thanks Richard, that's what i thought, with file handlings we can do
this..


Now that you've learned that, go look up "quine" on the Web.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Feb 9 '06 #8
Dev

Richard Heathfield wrote:
Dev said:
Thanks Richard, that's what i thought, with file handlings we can do
this..
Now that you've learned that, go look up "quine" on the Web.

Sorry, Richar, could u tell me what do u mean by "quine"
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


Feb 9 '06 #9
Dev said:

Richard Heathfield wrote:
Dev said:
> Thanks Richard, that's what i thought, with file handlings we can do
> this..


Now that you've learned that, go look up "quine" on the Web.

Sorry, Richar, could u tell me what do u mean by "quine"


It's something you look up on the Web. Consider the possibilities inherent
in a Web search engine such as Google or Altavista. Consider also the fact
that I would not have suggested that you look up the word "quine" if it
were not relevant to your question.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Feb 9 '06 #10
Richard Heathfield <in*****@invalid.invalid> writes:
Dev said:

Richard Heathfield wrote:
Dev said:

> Thanks Richard, that's what i thought, with file handlings we can do
> this..

Now that you've learned that, go look up "quine" on the Web.

Sorry, Richar, could u tell me what do u mean by "quine"


It's something you look up on the Web. Consider the possibilities inherent
in a Web search engine such as Google or Altavista. Consider also the fact
that I would not have suggested that you look up the word "quine" if it
were not relevant to your question.


He may be confused by the fact that the top site listed appears to be
for gay, black, disabled, Scottish women... :)

--

John Devereux
Feb 9 '06 #11
John Devereux:
Richard Heathfield:

that I would not have suggested that you look up the word "quine" if it
were not relevant to your question.


He may be confused by the fact that the top site listed appears to be
for gay, black, disabled, Scottish women... :)


Adding "source" to the search helps a lot.

Jirka
Feb 9 '06 #12
"Dev" <kr**********@gmail.com> writes:
Keith Thompson wrote:
"Dev" <kr**********@gmail.com> writes:
> Thanks Richard, that's what i thought, with file handlings we can do
> this..


One more time, please read <http://cfaj.freeshell.org/google/>
*before* you post here again. Thanks.


Hello Keith,
Thank for giving me link, i will follow the "Reply" format, sorry for
previous post, thanks once again..

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


Excellent, thank you.

One more thing: it's rarely necessary to quote the *entire* article to
which you're replying. You should delete anything that's not relevant
to your followup, while making sure that the attribution lines for
anythink you *do* quote are still there. Don't quote signatures
unless you're actually commenting on them.

Most posters here get this mostly right, so following their example is
a good approach.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 9 '06 #13
On Thu, 09 Feb 2006 12:42:02 +0000, in comp.lang.c , John Devereux
<jd******@THISdevereux.me.uk> wrote:
Richard Heathfield <in*****@invalid.invalid> writes:
Dev said:

Sorry, Richar, could u tell me what do u mean by "quine"


It's something you look up on the Web.


He may be confused by the fact that the top site listed appears to be
for gay, black, disabled, Scottish women... :)


Sure, but the /third/ entry looks kinda relevant (smiley noted...)
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Feb 9 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by alice | last post: by
13 posts views Thread by Peter Ammon | last post: by
2 posts views Thread by Slipperman | last post: by
16 posts views Thread by msundaram.visvanathan | last post: by
15 posts views Thread by vinay.khankari | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.