Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Efficient way to read exit codes (popen, system, exec?) 1

Status
Not open for further replies.

LenRI

Programmer
Jul 15, 2003
13
0
0
US
I'm calling a little C program (let's say PorgA) I wrote from a C++ application (call this one ProgB) which I am in the process of writing. I know I can talk between them by other means (signals, pipes, sockets, etc...), but I am simply trying to read the exit code of ProgA after calling it from ProgB. What would be in your opinion the best way to accomplish this. Mind you I not only need to get the exit code, but also the PID of ProgA whenever I launch it.

Thank you for your help.

Len
 
On POSIX machines you can examine the status after waiting for it exit:
Code:
if(fork() = 0)
{
  execv(fullpath,argv);//argv must be null terminated
//array of char*'s
  exit(1);
}
else{
int *status;
wait(status);
if(*status == 0)
  //exited correctly
else
  //error
}

or if your not intrested in forking (this should be less system dependant):

Code:
string command = "ls -al";
int exitCode = system(command.c_str());
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top