From 51a914441c5e13a8013536866b298b274d0f7ddc Mon Sep 17 00:00:00 2001 From: cat Date: Sun, 18 Jan 2026 06:59:13 +0200 Subject: [PATCH] self.cpp, a piece of code that makes itself --- self.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 self.cpp diff --git a/self.cpp b/self.cpp new file mode 100644 index 0000000..27cbe45 --- /dev/null +++ b/self.cpp @@ -0,0 +1,40 @@ +/* + * 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" + * 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 +#include +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\" \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 \n#include \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; +}