473,320 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Program that prints itself

gg
If somebody asks me to write a program that prints itself ( quine ) can
I write the following program as an answer -

#include <iostream>
#include <fstream>
using namespace std;

int main ( void )
{
ifstream self ( __FILE__ );

if ( !self )
{
cerr << " File: " << __FILE__ << "does not exist." << endl;
}

string s;

while ( !self.eof ( ) )
{
getline ( self, s, '\n' );

cout << s << endl;
}
}

May 11 '06 #1
9 4348
gg wrote:
If somebody asks me to write a program that prints itself ( quine )
can I write the following program as an answer -

#include <iostream>
#include <fstream>
using namespace std;

int main ( void )
{
ifstream self ( __FILE__ );
[..]


If I remember correctly, using __FILE__ does not qualify. FWIW, your
program does not have to exist as a file. Especially considering that
in your program, if the file doesn't exist (has been renamed, deleted,
moved, after compilation/linking), the program won't do what's required.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 11 '06 #2
gg wrote:
If somebody asks me to write a program that prints itself ( quine ) can
I write the following program as an answer -

#include <iostream>
#include <fstream>
using namespace std;

int main ( void )
int main() is the C++ way. Some consider the C way blasphemy.
{
ifstream self ( __FILE__ );

if ( !self )
{
cerr << " File: " << __FILE__ << "does not exist." << endl;
You could also do:

cerr << " File: " __FILE__ " does not exist." << endl;

But you omitted:

return -1;
}

string s;

while ( !self.eof ( ) )
{
getline ( self, s, '\n' );

cout << s << endl;
}
while( getline( self, s ) )
{
cout << s << '\n'; // No need to flush each time
}
}


May 11 '06 #3
On 11 May 2006 07:51:04 -0700, I waved a wand and this message
magically appeared from mlimber:
int main() is the C++ way. Some consider the C way blasphemy.


so int main(int argc, char *argv[]) blasphemy? Perhaps you'd prefer it
to be like this:

int main(std::vector<std::string> args)

It'd be much easier parsing arguments...

--
http://www.munted.org.uk

Take a nap, it saves lives.
May 11 '06 #4
gg wrote:
If somebody asks me to write a program that prints itself ( quine ) can
I write the following program as an answer -

<snip>

Only if it's in the request that the program needs only to be able to
run on an interpreter.

Once it's been compiled and the binary has parted from the source code,
it won't work anymore.

If you want some real quines, try here

http://smjg.port5.com/wwwep/quines/
http://en.wikipedia.org/wiki/Quine
ftp://quatramaran.ens.fr/pub/madore/selfrep/selfrep/
http://www.nyx.net/~gthompso/quine.htm

Stewart.

--
-----BEGIN META GEEK CODE BLOCK-----
Version: 1
gc
------END META GEEK CODE BLOCK------

My e-mail is valid but not my primary mailbox. Please keep replies on
the 'group where everyone may benefit.
May 11 '06 #5
Alex Buell wrote:
On 11 May 2006 07:51:04 -0700, I waved a wand and this message
magically appeared from mlimber:
int main() is the C++ way. Some consider the C way blasphemy.
so int main(int argc, char *argv[]) blasphemy?


No, the "void" in the OP.
Perhaps you'd prefer it
to be like this:

int main(std::vector<std::string> args)

It'd be much easier parsing arguments...


Sure, but that's not what I was getting at.

Cheers! --M

May 11 '06 #6
mlimber wrote:
Alex Buell wrote:
On 11 May 2006 07:51:04 -0700, I waved a wand and this message
magically appeared from mlimber:
int main() is the C++ way. Some consider the C way blasphemy.


so int main(int argc, char *argv[]) blasphemy?


No, the "void" in the OP.


Sorry, I should have said "abomination." See this FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-29.4

Cheers! --M

May 11 '06 #7
On 11 May 2006 08:07:26 -0700, I waved a wand and this message
magically appeared from mlimber:
No, the "void" in the OP.


Sorry, I should have said "abomination." See this FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-29.4


No, don't worry I hate the void main() abomination. How else are we
going to get a return result code? When I get a chance I'll shoot the
Microsoftite who decided it was alright to ignore exiting conventions.

--
http://www.munted.org.uk

Take a nap, it saves lives.
May 11 '06 #8
Alex Buell <al********@munted.org.uk> wrote:
On 11 May 2006 08:07:26 -0700, I waved a wand and this message
magically appeared from mlimber:
> No, the "void" in the OP.


Sorry, I should have said "abomination." See this FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-29.4


No, don't worry I hate the void main() abomination. How else are we
going to get a return result code? When I get a chance I'll shoot the
Microsoftite who decided it was alright to ignore exiting conventions.


Wrong void. He was talking about

int main(void)

versus

int main()

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 11 '06 #9
On Thu, 11 May 2006 20:42:29 +0000 (UTC), I waved a wand and this
message magically appeared from Marcus Kwok:
No, don't worry I hate the void main() abomination. How else are we
going to get a return result code? When I get a chance I'll shoot
the Microsoftite who decided it was alright to ignore exiting
conventions.


Wrong void. He was talking about

int main(void)

versus

int main()


sigh.

Perhaps I'm just tired.

--
http://www.munted.org.uk

Take a nap, it saves lives.
May 11 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
by: Xah Lee | last post by:
here's a large exercise that uses what we built before. suppose you have tens of thousands of files in various directories. Some of these files are identical, but you don't know which ones are...
2
by: Sara Khalatbari | last post by:
I'm writing a code that checks the header of .po file for syntax errors. I want this program to run msgfmt.py on the .po file first & then the rest. How can you write a code that runs another...
7
by: Prashanth Badabagni | last post by:
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");
6
by: Buck Rogers | last post by:
Hi guys! Love your work! The below program is from K&R2, p22. ================================= #include <stdio.h> /* count digits, white space, others */ main() {
10
by: Robert Rotstein | last post by:
Following is a C program, taken from http://en.wikipedia.org/wiki/Quine#Sample_quine_in_C, which has the curious property that, when executed, it produces its own source code as output. ...
16
by: Puneet | last post by:
Hi ALL, I have a silly question... (may be) Can we write a single line C program whose output is the program itself? Is anybody know the answer please tell me. Puneet
3
by: Double Echo | last post by:
Hi all, I'm using PHP 4.4.2, and use PHP on both the command-line and the web. I am running PHP on SuSE 10 Linux , in a VMware 5.5 workstation, using Apache 2.0.55 , on my Dell laptop. ...
3
by: haelly | last post by:
Write a program that prompts the user to enter three different integer values.If the values are not different, the program prints a message"equal values" and terminates(hint: use the return...
1
by: haelly | last post by:
write a program that prompts the user to enter three different positive integer values.If the values are not different, the program prints a message"equal value" and terminates(hint:use the return...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.