I want to turn off the buffering for the stdout for getting the exact result for the following code
while(1) {
printf(".");
sleep(1);
}
The code printf bunch of '.' only when buffer gets filled.
I want to turn off the buffering for the stdout for getting the exact result for the following code
while(1) {
printf(".");
sleep(1);
}
The code printf bunch of '.' only when buffer gets filled.
You can use the setvbuf function:
setvbuf(stdout, NULL, _IONBF, 0);
Here're some other links to the function.
setbuf(stream, NULL);
is equivalent to setvbuf(stream, NULL, _IONBF, BUFSIZ);
setbuf
or setvbuf
as the very first operation on a stream before any input or output is performed on it. Thus using fflush
explicitly is usually a better idea.
Oct 24, 2011 at 14:56
You can also use setbuf
setbuf(stdout, NULL);
This will take care of everything
Both ANSI C and the System V Interface Definition (Issue 2) require <<setbuf>>. However, they differ on the meaning of a <<NULL>> buffer pointer: the SVID issue 2 specification says that a <<NULL>> buffer pointer requests unbuffered output. For maximum portability, avoid <<NULL>> buffer pointers.
Mar 4, 2016 at 23:16
setbuf()
states: "setvbuf(stream, buf, _IONBF, BUFSIZ)
if buf is a null pointer." _IONBF
means unbuffered Both POSIX 7 and the C standard all agree, and have agreed for decades.
Feb 13, 2020 at 22:00
Use fflush(FILE *stream)
with stdout
as the parameter.
You can do this:
write(1, ".", 1);
instead of this:
printf(".");
Use fflush(stdout)
. You can use it after every printf
call to force the buffer to flush.