fflush(stdout)

This is a discussion on fflush(stdout) within the C Programming forums, part of the General Programming Boards category; Hi all, Just a quick one. I had been forced to use fflush(stdout) on certain instances. Whats i wanted to ...

  1. #1
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,725

    fflush(stdout)

    Hi all,

    Just a quick one. I had been forced to use fflush(stdout) on certain instances. Whats i wanted to know is, why doesn't the data doesn't get written unless i call the fflush(stdout) on certain cases. Is obvious symptoms for this cause?

    Thanks in advance!

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    24,858
    Quote Originally Posted by ssharish2005
    Just a quick one. I had been forced to use fflush(stdout) on certain instances. Whats i wanted to know is, why doesn't the data doesn't get written unless i call the fflush(stdout) on certain cases. Is obvious symptoms for this cause?
    The standard output stream is typically line buffered, so in the event that the buffer is not yet full or a newline is not yet encountered (or if there has not been intervening input), the output would remain in the buffer, hence fflush() is useful to flush the buffer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,725
    But what i thought was that printf itself internally would do that. I might wrong here.

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    24,858
    Quote Originally Posted by ssharish2005
    But what i thought was that printf itself internally would do that. I might wrong here.
    Your own experience tells you that you are wrong
    Of course, if the output stream is line buffered and you print a line then the buffer will be flushed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,725
    >Your own experience tells you that you are wrong
    haha i might be. But wasn't sure.

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ssharish2005 View Post
    >Your own experience tells you that you are wrong
    haha i might be. But wasn't sure.

    -ssharish
    Yep. That's why if you are trying to track a segfault using printf(); always use "printf();fflush(stdout);" -- otherwise, you can be misled as to where the fault occurs because the output was left in the buffer (implying, wrongly, that execution had not yet reached the printf()).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    24,858
    Quote Originally Posted by MK27
    That's why if you are trying to track a segfault using printf(); always use "printf();fflush(stdout);"
    It may be easier to just use fprintf() with the standard error stream, printing a new line at the end, since the standard error stream is guaranteed to not be fully buffered by default.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-21-2008, 09:42 PM
  2. fflush(stdout)
    By salvadoravi in forum C Programming
    Replies: 5
    Last Post: 01-21-2008, 02:58 PM
  3. while statement
    By wonderpoop in forum C Programming
    Replies: 13
    Last Post: 10-23-2006, 10:14 PM
  4. custom header file can someone help?
    By shaftinafrica in forum C Programming
    Replies: 16
    Last Post: 11-15-2005, 09:09 PM
  5. Animation not working....
    By aquinn in forum C Programming
    Replies: 7
    Last Post: 02-19-2005, 05:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21