program that prints its source code

  • Thread starter Prashanth Badabagni
  • Start date
P

Prashanth Badabagni

Hi ,
I'm Prashanth Badabagni .. I have and idea how a program prints
it's own source code ..

void main()
{
FILE *P;
char *file,c;
strcpy(file,__FILE__);
p=fopen(file,"r");
if(!p)
{
printf("error opening the file .....");
return 0;
}

while((c=fgetc(p)) != EOF)
printf("%c",c);
}
Is there any way .. that we can print the source code
of a program ...
Thanks ....
Prashanth Badabagni
(e-mail address removed)
 
J

Jack Klein

Hi ,
I'm Prashanth Badabagni .. I have and idea how a program prints
it's own source code ..

void main()

Undefined behavior, in C main() returns int in a hosted environment.
{
FILE *P;
char *file,c;
strcpy(file,__FILE__);

Undefined behavior, call to a function returning a type other than int
without a prototype in scope.
p=fopen(file,"r");

Undefined behavior, call to a function returning a type other than int
without a prototype in scope.
if(!p)
{
printf("error opening the file .....");

Undefined behavior, call to a variadic function without a prototype in
scope.
return 0;
}

while((c=fgetc(p)) != EOF)
printf("%c",c);

Undefined behavior, call to a variadic function without a prototype in
scope.
}
Is there any way .. that we can print the source code
of a program ...
Thanks ....

Even assuming that you included the appropriate headers, you are
assuming much more about __FILE__ than the C standard defines.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
A

Arthur J. O'Dwyer

Hi ,
I'm Prashanth Badabagni .. I have and idea how a program prints
it's own source code ..

Is there any way .. that we can print the source code
of a program ...
Thanks ....


Well, aren't you in luck that I just finally got around to uploading
my own little quine-generator on the Web! I encourage everyone to
take a look at

http://www.contrib.andrew.cmu.edu/~ajo/free-software/quine.c

and tell me what you think of it. Known things that will attract
flames like a moth:

* Fixed maximum line size
* Fixed maximum #include-section size
* 'quine' produces source code for the platform on which
'quine' was run, not portable code (i.e., running program
on ASCII platform produces ASCII-specific code)
* Will not compile out of the box on some systems; requires
the line
#define quine rand
to be inserted at the top of the file on those systems

Solutions to the third problem are welcome, BTW. :)
Ditto comments on the other C sources at that site.

-Arthur
 
M

Martin Ambuhl

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):
void main()
^^^^
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.
{
FILE *P;
^^^^
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:
p=fopen(file,"r");
^^
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.
return 0;

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
 
M

Malcolm

Prashanth Badabagni said:
I'm Prashanth Badabagni .. I have and idea how a program prints
it's own source code ..
What you are looking for is known as a quine.
It is not easy to write a program that prints its own source. Just passing a
string to printf() lands you in a recursive situation, since the string
itself is part of the source.
There are ways round this, though usually they are not completely portable
and rely on fancy tricks with ASCII codes and pointers.
 
C

Craig

Hi ,
I'm Prashanth Badabagni .. I have and idea how a program prints
it's own source code ..

void main()

int main()
{
FILE *P;
char *file,c;
strcpy(file,__FILE__);
p=fopen(file,"r");

p=fopen(__FILE__,"r");

Your version has undefined behavior here. The variable file is a
pointer to a character, but it points to some undefined location.
Trying to access that location (or asking strcpy to access that
location) is undefined behavior.
if(!p)
{
printf("error opening the file .....");
return 0;
}

while((c=fgetc(p)) != EOF)
printf("%c",c);

putchar(c);

Although what you have isn't necessarily bad, the suggested
alternative will likely be significantly more efficient.

You also should close your file:

fclose(p);
return 0; /* We are returning int after all... */
}
Is there any way .. that we can print the source code
of a program ...
Thanks ....
Prashanth Badabagni
(e-mail address removed)

In fact, a type of program known as a "quine" is a program that prints
its own source code as output. An understood requirement of such
programs is that they do not read in their source code from file.
Also, your program will fail if the source is not present when the
program is run.

Consider a google search on "quine" to find out more.
 
G

goose

Jack Klein said:
Undefined behavior, in C main() returns int in a hosted environment.


Undefined behavior, call to a function returning a type other than int
without a prototype in scope.

also, UB when writing to unallocated memory if the strcpy that gets
linked in is the standard library strcpy.


goose,
 
K

Kevin Easton

Malcolm said:
What you are looking for is known as a quine.
It is not easy to write a program that prints its own source. Just passing a
string to printf() lands you in a recursive situation, since the string
itself is part of the source.
There are ways round this, though usually they are not completely portable
and rely on fancy tricks with ASCII codes and pointers.

That's not at all necessary. The basic structure of a quine in C is
usually:

<Preamble>
<Define data>
<Output Preamble>
<Output defintion of data, using data>
<Output data>

Then you just have to pick the contents of data so that it'll print out
the code making up the three Output blocks, and write the "Output
defintion of data, using data" block so that it correctly handles
escape sequences. To keep this on topic, in C this usually means at
least replacing \ with \\, " with \" and ' with \' (since the definiton
of data is likely to make use of strings, which must themselves contain
\, " and ' characters (because the Output statements use those
characters)).

- Kevin.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top