Create a Quine, that is, a program that prints out its own source code. (Expert)

In this thread Read Me: C / C++ FAQ's and Practice problems started by ~s.o.s~ how do you create this program.

#include<iostream.h>
main()
{
char*s="#include<iostream.h>%cmain(){char*s=%c%s%c;cout.form(s,10,34,s,34,10);}
%c";cout.form(s,10,34,s,34,10);}

char*s="#include<iostream.h>%cmain(){char*s=%c%s%c;cout.form(s,10,34,s,34,10);}%c";
cout.form(s,10,34,s,34,10);}

What is the logic behind this?

#include<iostream.h>
main()
{
char*s="#include<iostream.h>%cmain(){char*s=%c%s%c;cout.form(s,10,34,s,34,10);}
%c";cout.form(s,10,34,s,34,10);}

char*s="#include<iostream.h>%cmain(){char*s=%c%s%c;cout.form(s,10,34,s,34,10);}%c";
cout.form(s,10,34,s,34,10);}


What is the logic behind this?

it is much easier to read if you add in all of the line breaks. they were removed in the examples to maintaine their quinness

the logic behind is simply that they are creating a char pointer "s" that contains all of the source in the document.

then using cout.form they print the output using formatting.

for some reason in VC++ i was unable to use cout.form but using printf worked just fine instead.

I tried it in my computer.

#include<iostream.h>
main(){char*s="#include<iostream.h>%cmain(){char*s=%c%s%c;cout.form(s,10,34,s,34,10);}%c";
cout.form(s,10,34,s,34,10);}

As you said a string C is created and the code is written to it.
But I thought that in a quine it automatically reads your code and displays it.

Here we write the code into the C variable.But suppose the code is very big, then we can't type everything into this variable.

cout.form didn't work for me either. I use borland C++

I tried it in my computer.

#include<iostream.h>
main(){char*s="#include<iostream.h>%cmain(){char*s=%c%s%c;cout.form(s,10,34,s,34,10);}%c";
cout.form(s,10,34,s,34,10);}

As you said a string C is created and the code is written to it.
But I thought that in a quine it automatically reads your code and displays it.

Here we write the code into the C variable.But suppose the code is very big, then we can't type everything into this variable.

cout.form didn't work for me either. I use borland C++

hehe if you want it to read a very large C++ file then just create an IO stream to read the CPP file contents and display them.

infact on the quine page i linked it is really nothing more than a trick here or there. for people using PHP they reccomend using PHP's ability to read files and have it read itself and output that.

Oh ok!
Is this code correct?

#include<iostream.h>
#include<fstream.h>

void main()
{
ifstream filein("code.cpp");;
char str[100];
while(!filein)
{
filein>>str;

}
cout<<str;
}

Correct me if you find a mistake in the code.

as long as the code.cpp file is located with the exe it will output whatever the source file is.


works well

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <vector>
using namespace std;
int main()
{
 ifstream openfile("./string_manip.cpp", ios::in);
 string linefromfile;
 while ( getline(openfile, linefromfile) )
 {
  cout << linefromfile << endl;
 }
 openfile.close();
 return 0;
}

These aren't examples of quines though - To qualify as a quine, the program must be able to replicate its own source code without any input (either from a file, the keyboard, or anywhere else)

see here for a little more info http://en.wikipedia.org/wiki/Quine_%28computing%29

shhhh we're having fun over here :)

the quine page i posted was pretty decent and has examples for a ton of languages should check it out as well :)

These aren't examples of quines though - To qualify as a quine, the program must be able to replicate its own source code without any input (either from a file, the keyboard, or anywhere else)

see here for a little more info http://en.wikipedia.org/wiki/Quine_%28computing%29

The link provided by bench has also the page link mentioned by killer_Typo .

@Bench

Bench has correctly quoted that a Quine is a program that does not take any input of any form of input stream, be it keyboard or file stream. A quine should be capable of replicating itself by creating its own source code without any external stimuli / data.

Which in essence does not seem to be that an easy trick. Any workaround wont qualify the program as a quine.

Abhijeet

Hi
I don't think what Bench said is possible (at least not in languages such as C or C++) since after you compile it the original language codes are lost and only the machine code is available in the executable file. Hence some information about the source code in the language in question should be present in it's code itself. However if printing the assembly language equivalent of a compiled source code qualifies as a quine, perhaps an executable file can be made to read itself

Best regards,
Mahaju

This article has been dead for over six months. Start a new discussion instead.