Files
Uncategorized/self.cpp

41 lines
2.0 KiB
C++

/*
* This is a piece of code that makes a copy of itself
* It is inspired by https://www.youtube.com/watch?v=QGm-d5Ch5JM
* Compile with GCC using "g++ self.cpp -o program"
*
* SPDX-FileCopyrightText: Dora "cat" <cat@thenight.club>
* SPDX-License-Identifier: MPL-2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <fstream>
#include <string>
std::string self = "/*\n * This is a piece of code that makes a copy of itself\n * It is inspired by https://www.youtube.com/watch?v=QGm-d5Ch5JM\n * Compile with GCC using \"g++ self.cpp -o program\"\n *\n * SPDX-FileCopyrightText: Dora \"cat\" <cat@thenight.club>\n * SPDX-License-Identifier: MPL-2.0\n *\n * This Source Code Form is subject to the terms of the Mozilla Public License,\n * v. 2.0. If a copy of the MPL was not distributed with this file, You can\n * obtain one at http://mozilla.org/MPL/2.0/.\n */\n\n#include <fstream>\n#include <string>\nstd::string self = \"!\";\n\nint main() {\n std::fstream file(\"self2.cpp\", std::fstream::out);\n for (char character : self) {\n if (character == 92) {\n continue;\n }\n if (character == 33) {\n for (char code : self){\n if (code == 10){\n file << (char)92 << (char)110;\n continue;\n }\n if (code == 34){\n file << (char)92;\n }\n file << code;\n }\n continue;\n }\n file << character;\n }\n return 0;\n}\n";
int main() {
std::fstream file("self2.cpp", std::fstream::out);
for (char character : self) {
if (character == 92) {
continue;
}
if (character == 33) {
for (char code : self){
if (code == 10){
file << (char)92 << (char)110;
continue;
}
if (code == 34){
file << (char)92;
}
file << code;
}
continue;
}
file << character;
}
return 0;
}