470,855 Members | 1,301 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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 4138
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Sara Khalatbari | last post: by
7 posts views Thread by Prashanth Badabagni | last post: by
6 posts views Thread by Buck Rogers | last post: by
10 posts views Thread by Robert Rotstein | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.