dodle
June 14th, 2012, 06:01 AM
I'm using popen to open a subprocess, but I don't know how to get the program to wait until the process finishes to continue. I've searched the forum and googled "c popen wait" but can't find an answer. The system command works for this but I have been warned not to use it.
#include <stdio.h>
int main()
{
popen("sleep 4 && echo done! 2>&1", "r");
printf("Subprocess finished.\n");
return 0;
}
Is there a way to wait for popen to finish, or is there a better way to open a subprocess?
----- Solved -----
Ah, figured it out:
#include <stdio.h>
int main()
{
FILE* test = popen("sleep 4 && echo done! 2>&1", "r");
pclose(test);
printf("Subprocess finished.\n");
return 0;
}
However, I'm having an issue getting popen to print the output of the process to stdout.
#include <stdio.h>
int main()
{
popen("sleep 4 && echo done! 2>&1", "r");
printf("Subprocess finished.\n");
return 0;
}
Is there a way to wait for popen to finish, or is there a better way to open a subprocess?
----- Solved -----
Ah, figured it out:
#include <stdio.h>
int main()
{
FILE* test = popen("sleep 4 && echo done! 2>&1", "r");
pclose(test);
printf("Subprocess finished.\n");
return 0;
}
However, I'm having an issue getting popen to print the output of the process to stdout.