As the existing comments have said, stdout is probably block-buffered because it isn't opened to a terminal any more. Try adding the call:
setvbuf(stdout, NULL, _IOLBF, 0);
somewhere around the start of the program. This will change stdout to line-buffered mode, which will flush the output as soon as the program writes a newline.