Enjoy coding
Enjoy coding

Reputation: 4366

How to read output and give input to a program from c program?

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

Upvotes: -1

Views: 2286

Answers (2)

Martin Beckett
Martin Beckett

Reputation: 96109

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

Upvotes: 3

Jon Watte
Jon Watte

Reputation:

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.

Upvotes: 1

Related Questions