Sign in to follow this  

printf appears to be buffering output

This topic is 4306 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay this is annoyingly me immensely; For some reason (in c) my printf statements have been changed so that instead of outputting whatever I tell them to, they seem to 'buffer' the output until I pass a newline, as in, it doesn't print anything until I pass something with '\n' in. This is immensely annoying as I want to build stuff up and display progress dots, and none of the dots show until the process is complete. Case in point: code:
     printf(".");
is the exact line and nothing prints out until a later line passes a '\n' character. How can I get rid of this behaviour and return it to it's old ways? (why did it start?)

Share this post


Link to post
Share on other sites
Indeed, printf() does buffered i/o - it has been buffering since its first implementation. In fact, this is the case of all the stdio functions. To bypass buffering, you must use either fflush() or remove the buffering using setvbuf()


setvbuf(stdout, NULL, _IONBF, 2);


(the 2 at the end is the size of the buffer ; passing a NULL buffer will cause the system to allocate one, unless the _IONBF mode is set (non-buffered). The size is supposed to be no less than 2 so I init it to 2 (I can't test the behavior of this code if the size is set to 0)).

HTH,

Share this post


Link to post
Share on other sites

This topic is 4306 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this