Program that prints itself

G

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;
}
}
 
V

Victor Bazarov

gg said:
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
 
M

mlimber

gg said:
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
}
 
A

Alex Buell

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...
 
S

Stewart Gordon

gg said:
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.
 
M

mlimber

Alex said:
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
 
M

Marcus Kwok

Alex Buell said:
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()
 

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

Similar Threads


Members online

No members online now.

Forum statistics

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

Latest Threads

Top