Jump to content
  • Advertisement
Sign in to follow this  
Xgkkp

printf appears to be buffering output

This topic is 4521 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
Advertisement
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
If you absolutely have to, you could flush stdout with: fflush(stdout);

*edit* too slow ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!