2

Basically I am making a Python program and part of it needs to run a C++ executable, I am calling the exe with:

subprocess.call(["C:\\Users\\User\\Documents\\Programming\\Python\\Utilities\\XMLremapper\\TranslatorSource\\FileFixer.exe", "hi"])

But how do I make the C++ program read the input? I tried:

FILE * input = popen("pythonw.exe", "r");
cout<< input.getline() << endl << endl;

But that just outputs 0x22ff1c and definitely not "hi". What code is needed to pipe the input into the C++ program?

2 Answers 2

4

They are passed as parameters to the main function.

main(int argc, char *argv[])

argc is the length of argv. So it would be

main(int argc, char *argv[])
{
 cout<<argv[1];
 return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

ok, I tried this and it told me the address of the exe. how is the "hi" passed from the python command accessed, or do I need a different python command. ps. I removed the FILE * input = popen("pythonw.exe", "r") before trying your code, should I have left it in? Thanks, Jamie
My mistake. argv[0] is program name, argv[1] is the first parameter. You don't need any FILE variable because everything whats your program get is in argv.
1

If you just want to pass in a few arguments, an easy option is to read arguments on the command line, as suggested in another answer.

For more substantial input/output, where you'd naturally want to use cout or cin, a better option is to use subprocess.Popen. You can then write to and read from the other process as if they were file handles in python. For example:

proc = subprocess.Popen(["FileFixer.exe"], 
            stdin=subprocess.PIPE, 
            stdout=subprocess.PIPE, 
            stderr=subprocess.PIPE)
stdout, stderr = proc.communicate("hi\n") 

This tells python to run the process, passing in 'hi' followed by carriage return as standard input, which can then be read by cin in the C++ program. Standard output (the result of cout in C++) is then passed into the stdout list, and standard error is passed into stderr.

If you need more interactive communication, you can also access proc.stdout and proc.stdin as if they were filehandles in python (e.g. proc.stdin.write("hi\n")

1 Comment

so C++ would send out a message to python to give it the next arguement and then wait for cin >> a; or simmilar?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.