IPC Question

I have 2 Programs. One program will start the other and should block for input, read the input, block again and so on:
But I only get the input, when all the cout lines are written. What do I do wrong?

int main(int argc, char **argv)
{
FILE *read_fp;
char buffer[BUFSIZ+1];

int chars_read;

memset(buffer, '\0', sizeof(buffer));
read_fp = (FILE*) popen("../PipeChild/Debug/PipeChild", "r");
if(read_fp != NULL)
{
while(1)
{
chars_read = fread(buffer, sizeof(char), BUFSIZ, read_fp);
if(chars_read > 0)
{
printf("Output is:\n%s\n",buffer);
}
}
pclose(read_fp);
exit(EXIT_SUCCESS);
}
exit(EXIT_FAILURE);

}

Program 2-->

#include <stdio.h>
#include <unistd.h>
#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
sleep(5);
cout << "Hallo \n";
sleep(5);
printf("Test \n");
sleep(5);
printf("1 \n");
}

Thank you very much.
Georg
stdout is (usually) buffered by default. Try setvbuf(stdout,NULL,_IONBF,0); before you do any output to stdout. Or just do fflush(stdout); after outputting a line.

EDIT: Just remembered that std::endl will also flush the stream - so cout << "Hallo " << endl; should do the trick.
Last edited on
Topic archived. No new replies allowed.