i dont understand what the function "flush" does
please answer
and one more,
is there a way that we output a set of data
only after a certain while in program execution
without storing it in a file and again
printing it after reading from that file
eg....
i declared a variable 'va'
in c++ code ..the value of va changes based upon
the input given by user which is done many times in program
and i like to print the value of va many times in program
but,only after the user is satisfies with his number of inputs
so i need to output all the values va all at a time at the only at the end

Recommended Answers

All 10 Replies

The flush function forces data to be written to a disk data file (or some other output stream).

There is a couple ways I can think of to solve your problem:
1. write each value to a file then read the values back and display them when the user is done.

2. Save the values in a vector or some other kind of array/container.

but using cout or any object of ofstream also makes
the data to be outputted what difference does flush make

>but using cout or any object of ofstream also makes the data to be outputted
You're mixing things up. Any object derived from ostream contains a buffer. It's this buffer that "makes the data to be outputted", and when it sends characters from the buffer to the destination (the destination could be a file, your monitor, or something else), we call this a flush. Now, the buffer flushes itself periodically without intervention from you (such as when it fills up or you request input from a tied stream). However, if you want the buffer to flush itself outside of those times, the flush member function is there so that you can force a flush.

So the difference is that flush gives you more control over when the buffer is flushed, rather than waiting until the implementation is ready.

i think i got it,
now ,is there a way that we stop flushing action of
output stream buffer for a while either by
........increasing the capacity that the buffer can hold
or
.........by "removing any ties" that the ostream object has
with istream objects

> ........increasing the capacity that the buffer can hold
lookup std::basic_ios<>::rdbuf and std::basic_streambuf<>::pubsetbuf

> .........by "removing any ties" that the ostream object has
calling std::basic_ios<>::tie(0) will remove any tie.

now,
if i have a code whose only
only work is to print "hello world"
to the monitor
and in that code i increased the capacity
of the buffer so that it is not saturated
by the length of statement "hello world"
and i even do not remove ties
of that ostream (cout in this case)object with
istream objects(cin in this case)
then is it not possible to display output
without using 'flush '
__________________________________________________________
And to what extent can i increase the capacity of buffer?
____________________________________________________________

>then is it not possible to display output without using 'flush'
What in the world are you talking about? The only way to send output to the destination is to flush the buffer, and the buffer gets flushed automatically according to certain rules. You only need to call the flush member function when the automatic flushing isn't good enough.

>And to what extent can i increase the capacity of buffer?
You can replace the buffer with your own, thus the extent is limitless.

narue,
you told me of two cases when buffer gets flushed (when buffer gets filled up or while input is from a tied stream )
tell me the other cases
if these are the only cases
and if input is not from tied stream
then buffer should not get flushed until "saturated "
and what if i increase the capacity of buffer !!!?

>tell me the other cases
It depends on the stream. In the case of cout, you can expect it to be flushed when the program ends.

>then buffer should not get flushed until "saturated "
That's a reasonable assumption.

>and what if i increase the capacity of buffer !!!?
Then it will take longer to get flushed, perhaps? This isn't rocket science.

> i declared a variable 'va'
> in c++ code ..the value of va changes based upon
> the input given by user which is done many times in program
> and i like to print the value of va many times in program
> but,only after the user is satisfies with his number of inputs
> so i need to output all the values va all at a time at the only at the end

this was your original problem. and dragon gave you a couple of reasonable solutions. all your questions about flush and stream buffers have been taking you farther away from a solution to the actual problem that you are facing.
a. narue informed you that flushing of the stream does happen automatically even if you do not call flush.
b. setting the buffer size to a large value by something like char buf[1024*128] ; cout.rdbuf()->pubsetbuf(buf,sizeof(buf)); does not guarantee that the stream will not get flushed. the c++ standard only defines the behavior of pubsetbuf(0,0); this has the effect of turning off any buffering. pubsetbuf() called with any other values may or may not change the flushing behavior of the stream.
usually, calls to pubsetbuf() with non-zero values modify the flushing behaviour for filebufs (used with file streams). the specific behavior has to be defined in the documentation of the implementation (it is "implementation defined"). and you could look this up in the documentation provided by the specific implementation you are using.
c. the only portable way by which you can guarantee the behaviour you are looking for is by creating your own stream buffer class. this is not a difficult task; you can inherit all the functonality from a std::streambuf and modify only the flushing behaviour.

however, there are much easier ways to solve the problem; eg. buffer all your output to a stringstream till you are ready to display it to the user. at that point, dump the contents of the stringstream on standard output.

#include <iostream>
#include <sstream>
using namespace std ;

int main()
{
  ostringstream stm ;
  int va = 1 ;
  for( int i=0 ; i<10 ; ++i )
  {
    va *= 2 ;
    stm << i << ". " << va << '\n' ;
  }
  stm.flush() ;
  cout << "now we can print it out!\n" << stm.str() ; 
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.