On Tue, 12 Jul 2005, Alon Altman wrote:

Hi,
 I have some strange behavior of a C program.
The code is simple:

#include <stdio.h>

int main(void)
{
  char input[255];
  printf("Hello\n");
  gets(input);
  return 0;
}

When I compile this into a file called "prog" and then run: "./prog|cat" I
see that "Hello" is not printed until I give it input. Where is the buffer
here and is there any way to bypass it?

add fflush(stdout); after printf. The buffer is related to the stream stdout. You can turn it of using setvbuf() and friends. Don't do it if you don't have to. The pipes are set up by the shell and may defeat what you are trying to do with flush and setvbuf(). You can also use a fifo (mkfifo) instead of a pipe.

Peter

=================================================================
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]

Reply via email to