Click here to Skip to main content
15,903,203 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a console program that should create another exe and, after, run it .
The problem is that I have no idea how to do this, using fstream

What I have tried:

I know how to create other type of file (like document files)but no idea how to create exe file in this way I
Posted
Updated 1-Oct-20 23:38pm

fstream is not the problem: pass it an array of bytes and a path, and tell toi to write them.

You problem is the executable part of it.

Creating an exe file isn't difficult: it's a very simple file format. But ... what it contains is machine code instructions in binary format, and to create one from scratch means you have to know the machine code (and pretty damn well) and then write code to generate the correct machine code to do whatever function your EXE file is supposed to do when it runs. And from scratch, that's decidedly non trivial as you have no access to library functions, which means you need to produce those s part of the EXE you write and in the right place in the file (from an internal memory point of view).

Then you have to run it. That is trivial: google will tell you that.

Would I want to do it? No. And I've got a reasonable amount of experience in similar things over the decades I've been doing this. From this and your previous question you don't sound like you have anywhere near the skill and knowledge level developed that you would need to do this without a lot of time and frustration.

I don't know what exactly you are trying to do, but I'm pretty sure you are going the wrong way, or trying to run before you can walk!
 
Share this answer
 
A working example (compile it with your favourite compiler/IDE to obtain the executable):
C++
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
  const string Filename{"out.txt"};
  {
    ofstream ofs{Filename};

    ofs << "hi there" << endl;
  }

  ifstream ifs(Filename);

  while (ifs)
  {
    string line;
    getline(ifs, line);
    cout << line << endl;
  }
}
 
Share this answer
 
Please reask for your task because it sounds a bit silly. You may use CopyFile to copy some file or use DownloadFile to download some file from some webserver.

When executing an exe than take care about the correct working directory and access rights like explained in this article.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900