Link to home
Start Free TrialLog in
Avatar of axd
axd

asked on

How to call a seperate C program from another?

Easy enough to do in COBOL, but I cannot find any examples of how it is done in C. Can someone please give me a code example of how I can call another C program (a completely self contained program that has its own main function), and check the results in the calling program.

Thank you for your help.
Avatar of yonat
yonat

system("myprog.exe");
Avatar of ozo
In what manner are the results given?
system should return the exit status of the program.
>>In what manner are the results given?
>>system should return the exit status of the program.

Can save results in File and read from 1-st Programm.
If the results are giben on stdout, you might
 system("myprog.exe > File");
or perhaps
 File = popen("myprog.exe", "r");
Use any of spawn.. functions (if you want to return to the calling program), or the exec.. if you want to transfer. These functions do not incure the memory overhead of loading the command processor (which system() does). You have the option of using the path, specifying a custom env, and picking up the result.
On the other hand, spawn is not part of ANSI C, so it may not be as portable as system
True, but then that's what using C is all about. Most of my experience is with Intel and MS stuff, and of the 5 or 6 compilers I've really worked with, all had the functions. If you're tight on mem, chances are you have the spawn.. stuff. Using windows/Unix'es memory probably isn't your main consideration.
keep in mind... if you need to pass multiple arguments to the child process, the spawn function is much more flexible...not to mention less memory ungry...
ASKER CERTIFIED SOLUTION
Avatar of hougaard
hougaard

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial