-2

I am trying to use C++ to create as many copies of a .exe as I need. The original .exe won't be accessible, so I want to paste its code into the program before I compile it.

#include <fstream>
#include <iostream>
#include <string>

int main()
{

    for (int i = 1; i <= 5; ++i) {
        std::ofstream fout;
        fout.open("test-" +
                   std::to_string(i) + ".exe");

        if (!fout) {
            std::cerr << "Error\n";
            return 1;
        }
        fout << //"contents of exe";
        fout.close();
    }
}

I want the already compiled .exe code to be pasted where the fout << //"contents of exe"; is, but I can't figure out how to do it.

Any help would be appreciated, tysm!

  • 4
    If there were *ever* an example of an [XY problem](https://xyproblem.info/), this is it. – WhozCraig May 06 '22 at 03:39
  • In this case, you want to directly embed a copy of an executable file in your code. A possible solution is that you should convert each bit of your .exe to a character of "0" or "1" then write to a text file. Hence, you can copy the whole file content your code as a string. – cao-nv May 06 '22 at 03:42
  • Or use a hexdump utility like [`xxd -i`](https://stackoverflow.com/questions/1155578/which-program-creates-a-c-array-given-any-file), which will literally generate a hexabyte array that you can include directly in a source file. – WhozCraig May 06 '22 at 03:52
  • Don't store the desired `.exe` file's content in code at all. Store it in the resources/assets of the newly compiled `.exe` instead. Then the code can simply extract the resource/asset at runtime when needed. – Remy Lebeau May 06 '22 at 09:13

0 Answers0