I have a Python script that I've made to convert the output file of a C++ program. I'd like to have C++ call the script but can't seem to find a good way to do this.
I'm not trying to embed or extend Python code in C++. I just want to be able to call the Python script from within C++ and pass it arguments.
For example:
1 2 3 4 5 6 7 8 9
int main()
{
string filename;
doesSomethingCool();
writesFile(filename);
// Call the Python script passing a filename argument.
niftyPythonScript(filenameToConvert);
}
Obviously this code won't work...it's just an example of what I'm hoping to do. Is there a way to call niftyPythonScript.py with the argument filenameToConvert?
What if the location of the script is in an arbitrary location (but still known). Suppose it's always in /vol/data/files/niftyPythonScript.py...
Suppose I know that the Python scripts are in the same directory as the C++ file.
Example:
Assume myCode and niftyPythonScript.py are both contained in /home/username/someDirectory/anotherDirectory/. I'd like the following two commands to work the same.
I think the ./scriptname is relative to the C++ executable that uses it. It should be relative by default, regardless of where you execute the C++ program from, as long as the C++ program is in the right place.
In short: Those two commands should do the same thing. If they don't then I am confused.
system command only works if your C++ program don't intend to receive any input based on the Python script output.
That is, Python script will output 1,2,3 and this is needed as input by C++ program. To enable that, you may want to consider fork (expensive), pipe, socket, msg etc API calls instead. (I assume you using Linux environment).