My C++ program creates a subprocess to call a python script that does not terminate, but I also want to know what's happening in that subprocess by retrieving its (the python subprocess) stdout in my main program C++. I have the following code. In this case, I am only interested in the first line of stdout of the python program.
FILE *fp;
char *line = (char*)malloc(1024 * sizeof(char));
int index = 0;
char c;
fp = popen("python testing.py run_for_long_time", "r");
while (true)
{
c = fgetc(fp);
if (c == '\n') {
break;
}
line[index++] = c;
}
line[index] = '\0';
printf("Finished\n");
I noticed that the code does not print out finished until the subprocess has terminated (but I am only interested in the first line of the subprocess stdout so I don't want to wait for subprocess termination). How can I do this with popen and file descriptor?