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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>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.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
> ........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.
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
>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
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>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.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
> 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 ofpubsetbuf(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() ;
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287