2 Replies - 2835 Views - Last Post: 08 February 2011 - 10:23 AM Rate Topic: -----

#1 Guest_question*


Reputation:

why flush a stream?

Posted 08 February 2011 - 09:26 AM

im curious as to the point in flushing streams? why is it necessary to do this? i never find input being left inside a buffer and needing to get forced to my output display :/
Is This A Good Question/Topic? 0

Replies To: why flush a stream?

#2 b0ng01  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 38
  • View blog
  • Posts: 169
  • Joined: 16-July 10

Re: why flush a stream?

Posted 08 February 2011 - 10:17 AM

You ever use a stream connected to another stream.

Whenever I have had a need to force a stream, I was working on something where the stream could continue to be filled, but I wanted to force it to return everything. The most recent real world example I can think of, I created an irc bot. I had to force the stream to get everything out of the buffer.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 4141
  • View blog
  • Posts: 12,892
  • Joined: 25-December 09

Re: why flush a stream?

Posted 08 February 2011 - 10:23 AM

For the standard input and output streams.

In C the only time you will need to "flush" the input stream is when you are mixing scanf, with fgets because you may be leaving the end of line character in the buffer. For output buffers the only time you need to flush is to insure data is written immediately.

In C++ like C you will need to "flush" the input stream is when you are mixing get(), getline() with the normal cin extraction operator ">>". The output buffers don't change.


Now by flushing of the input buffers I do not mean using the fflush() function as this function is undefined for input buffers. I mean calling getchar() in a loop for C or cin.ignore(100,'\n'); for C++.


Jim

This post has been edited by jimblumberg: 08 February 2011 - 10:24 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1