Quine Program

I read about quines on Wikipedia and thought they were kind of interesting. I made a program and I wanted to know if it is considered a quine.

#include <fstream>
#include <windows.h>
using namespace std;
int main()
{
system("@echo off");
ofstream a_file("example.cpp");
a_file << "#include <fstream>\n";
a_file << "#include <windows.h>\n";
a_file << "using namespace std;\n";
a_file << "int main()\n";
a_file << "{\n";
a_file << "\tsystem(\"@echo off\");\n";
a_file << "\tofstream a_file(\"example2.cpp\");\n";
a_file << "\ta_file << \"#include <fstream>\\n\";\n";
a_file << "\ta_file << \"#include <windows.h>\\n\";\n";
a_file << "\ta_file << \"using namespace std;\\n\";\n";
a_file << "\ta_file << \"int main()\\n\";\n";
a_file << "\ta_file << \"{\\n\";\n";
a_file << "\ta_file << \"\tsystem(\\\"@echo off\\\");\\n\";\n";
a_file << "\ta_file << \"\tofstream a_file(\\\"example2.cpp\\\");\\n\";\n";
a_file << "\ta_file << \"\tsystem(\\\"pause\\\");\\n\";\n";
a_file << "\ta_file << \"\treturn 0;\\n\";\n";
a_file << "\ta_file << \"}\";\n";
a_file << "\tsystem(\"pause\");\n";
a_file << "\treturn 0;\n";
a_file << "}";
system("pause");
return 0;
}

>>>>>>>>>>>>>>> example.cpp <<<<<<<<<<<<<<<
#include <fstream>
#include <windows.h>
using namespace std;
int main()
{
system("@echo off");
ofstream a_file("example2.cpp");
a_file << "#include <fstream>\n";
a_file << "#include <windows.h>\n";
a_file << "using namespace std;\n";
a_file << "int main()\n";
a_file << "{\n";
a_file << " system(\"@echo off\");\n";
a_file << " ofstream a_file(\"example2.cpp\");\n";
a_file << " system(\"pause\");\n";
a_file << " return 0;\n";
a_file << "}";
system("pause");
return 0;
}

>>>>>>>>>>>>>>> example2.cpp <<<<<<<<<<<<<<<
#include <fstream>
#include <windows.h>
using namespace std;
int main()
{
system("@echo off");
ofstream a_file("example2.cpp");
system("pause");
return 0;
}

Also, since quines print the source code of the program, how do viruses replicate themselves if they are already compiled?
I'm not entirely sure what you're asking, finding out if a program is a quine is just a matter of running it and checking :P
Last edited on
closed account (Dy7SLyTq)
why not just have it open itsself and read every file of the line?
why not just have it open itself and read every file of the line?

Because then it's not a quine. A quine can't take any form of input.
I made a program and I wanted to know if it is considered a quine.

No. The source you're writing out is not the same as the original source.

how do viruses replicate themselves if they are already compiled?

Viruses replicate at the machine code level; they add new code to the host program and modify the existing code to reroute calls to their own proceedures.

Andy
Last edited on
With the advent of raw string literals, it's easy to write quite readable quines.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>

const std::string text = R"quine(
        const std::string prefix = "#include <iostream>\n"
                                   "#include <string>\n\n"
                                   "const std::string text = R\"quine(" ;
        const std::string suffix = ")quine\";\n\n";
        const std::string quine = prefix + text + suffix + text ;
            //         _
            //        / )
            //       / /
            //      / /               /)
            //     / /     .-```-.   / ^`-.
            //     \ \    /       \_/  (|) `o
            //      \ \  /   .-.   \\ _  ,--'
            //       \ \/   /   )   \( `^^^
            //        \   \/    (    )
            //         \   )     )  /
            //          ) /__    | (__
            //         (___)))   (__)))
            //
        int main() { std::cout << quine ; }

)quine";


        const std::string prefix = "#include <iostream>\n"
                                   "#include <string>\n\n"
                                   "const std::string text = R\"quine(" ;
        const std::string suffix = ")quine\";\n\n";
        const std::string quine = prefix + text + suffix + text ;
            //         _
            //        / )
            //       / /
            //      / /               /)
            //     / /     .-```-.   / ^`-.
            //     \ \    /       \_/  (|) `o
            //      \ \  /   .-.   \\ _  ,--'
            //       \ \/   /   )   \( `^^^
            //        \   \/    (    )
            //         \   )     )  /
            //          ) /__    | (__
            //         (___)))   (__)))
            //
        int main() { std::cout << quine ; }

http://ideone.com/4PULUU
Topic archived. No new replies allowed.