How to read output and give input to a program fro

2019-09-11 02:43发布

问题:

This is with reference to the below question:

Execute program from within a C program

How do I do the same on Windows with Tiny C Compiler? I need to execute a .exe fro c program and give input to it from within the same C program by using a file or string as source and read the output from it into a string or file.I have been using system() frunction. Any suggesstions or examples are welcome..Thanks in advance

回答1:

The simplest way if you don't have popen() etc, or you want to avoid the complexity, is to simplly write a data file eg. infile with fwrite() execute the external program with system() and then read outfile.

system("prog.exe <infile >outfile") 

Your prog.exe only has to read stdin and write stdout.

This way you can easily test it with the contents of in/out file. You would normally do this in your tmp directory and then delete them when you are finished.

The only thing to be careful of is the path to the .exe



回答2:

Google for "windows popen" and you find this link:

lists.trolltech.com/qt-interest/1999-09/thread00282-0.html

The C runtime library also has _popen(), but I would recommend against it.