Execute external file from inside C++ I am trying to create an application that on the front end presents the user with a text editor to input code into and then I will run that code and return the result. I thought it would be a fun project to try and build my own version of leetcode as a learning project.
Right now this is what I am doing to run the provided code. Let's say we are running python code, because that's all I have implemented right now.
First I take in the code that the user submits and create an a file that contains the given code:
std::string python(std::string code){
    std::string langCommand = &quot;python3 &quot;;
    std::string outFile;

    //I am hoping to parallelize this operation so I add threadID to output
    outFile = createOutFileName(&quot;PythonRunner.py&quot;);

    std::ofstream output;
    output.open(outFile);
    output &lt;&lt; code;
    output.close();
    return langCommand + outFile;
}

The next thing I do is create an output file and run the previously created file but I send my stdout/stderr to another outputfile:
std::string Program::run(){
    std::string command = createFile(this-&gt;lang, this-&gt;code);

    this-&gt;outputFile = createOutFileName(&quot;output.txt&quot;);

    std::stringstream newCommand;
    newCommand &lt;&lt; command;
    newCommand &lt;&lt; &quot;&gt;&gt; &quot;;
    newCommand &lt;&lt; outputFile;
    newCommand &lt;&lt; &quot; 2&gt;&amp;1&quot;;

    system(newCommand.str().c_str());
    std::string output = getOutputFileData(this-&gt;outputFile);
    cleanupFiles(command);
    return output;
}

Finally I return whatever I got from my output file and that is how I am executing my code.
I gotta think there is an easier way to do this. Especially since I am doing so much writing to a file and then reading from it is there anyway to get rid of that?
I also want to include more than one language in the future so I don't want to use any libraries that are specific to a certain language.
Lastly, this is my first C++ project so I would love any C++ tips!
Edit: I do want to eventually parallelize this code and find some way to encapsulate the program so it can't damage the system it is running on. If there is maybe some external program that would be good for that let me know and also gives me its stderr/stdout let me know.
Edit: As someone asked, here is the entire repo
https://github.com/lkelly93/coderunner
Rather than system() you should popen().
The difference is that system runs the command in a sub process with no access to this processes, while popen runs the command in a sub processes but provides accesses to the input and output streams of the sub processes.
This will allow you to run the sub-processes and stream input to the processes directly (from the input field you provided for standard input) and then read output from the processes and write it to the output field in your user interface.
FILE*  proc = popen(command);
std::string inputFromUser = getUserInputFromUI();
// Using fwrite() correctly left to user.
// You need to check for errors and continue etc.
fwrite(inputFromUser.c_str(), 1, inputFromUser.size(), proc);

char  buffer[100];
std::size_t size;
while((size = fread(buffer, 1, 100, proc)) != 0) {
    // Check for read errors here.
    sendToUserInterface(std::string(bufffer, buffer + size));
}

pclose(proc);
 


Sorted of related you don't need to save your pythong script as a file. The python command accept the - as a name which means read the script from the standard input rather than from the named file.
So you can run the python command (with popen()) then write the script you want to execute to the input stream of the file produced.
This will remove the need for any intermediate files.