Flushing the stream.

I am reading http://www.cplusplus.com/doc/tutorial/basic_io/ right now and it says:
The endl manipulator produces a newline character, exactly as the insertion of '\n' does; but it also has an additional behavior: the stream's buffer (if any) is flushed, which means that the output is requested to be physically written to the device, if it wasn't already. This affects mainly fully buffered streams, and cout is (generally) not a fully buffered stream. Still, it is generally a good idea to use endl only when flushing the stream would be a feature and '\n' when it would not. Bear in mind that a flushing operation incurs a certain overhead, and on some devices it may produce a delay.


can someone explain to me what flushing a stream and flushing operation means?
(it's about endl and '\n')

Last edited on
Flushing a stream simply means that the current contents of the buffer are written to the device that the stream is representing (terminal, disk, etc).
by device you mean another computer? what does "flushing a buffer" mean?
Last edited on
No. As I indicated in parenthises, streams are typically terminals or disk files.

You may be thinking of TCP/IP sockets which are sometimes referred to as a stream of data, but are not supported by std::streams.
what does "flushing a buffer" mean?

Instead of sending the output directly to the device, it is first stored in a buffer. A buffer is simply a block of memory. When the buffer is full, the entire contents are sent to the device in one go. Flushing simply means forcing this to happen whether or not the buffer is full.
AbstractionAnon, could you give an example of flushing a stream? and what does
which means that the output is requested to be physically written to the device, if it wasn't already.
mean?
Last edited on
1
2
  cout << "Hello world";
  cout << endl;


When line 1 is executed, "Hello world" is stored in cout's buffer. It is not written to the terminal;

When line 2 is executed, two things happen.
1) a '\n' is written to cout's buffer.
2) cout's buffer is flushed. This means the characters accumulated in cout's buffer are written to the console.

Streams are generally buffered because unbuffered I/O is very inefficient, particularly with disk files. You don't want to be doing a physical write to disk for every field in a cout statement.
Okay, can you also explain how a string can be treated as a stream?
It isn't. There are stream types that use a string, where other streams have a file or terminal.
http://www.cplusplus.com/reference/sstream/
You're referring to a stringstream.
http://www.cplusplus.com/reference/sstream/stringstream/

Works like an ordinary stream, except that the underlying "device" is a std::string.

1
2
3
4
5
  stringstream ss;

  ss << "hello world";  // Copies the literal to ss's buffer
  ss << endl;  // Flushes the stringstream.  i.e. "hello world" is written to the string underlying ss.
  cout << ss.str() << endl;  // Copy the underlying string to cout 


i understand input and output streams from the keyboard to the program and from program to the device? but i dont understand the string stream, can someone explain it or just explain exactly what a stream is / does in general?
Last edited on
A stream is really just a fancy buffer. When you use the >> operator, you're reading characters out of the buffer. When you use the << operator, you're writing characters to the buffer. When the buffer is full, or you use endl, the buffer is written to the underlying device.

stringstreams work the same way, except that the "underlying device" is a string. stringstreams are very handy for performing formatted output to a string. With a simple std::string, you can append data to the string, but a std::string doesn't support the level of custom formatting that a stream does. With a stringstream, one can format output to a string using the same iomanipulators that any stream supports (width, fill, left, right, dec, hex, prec, etc). Once you've formatted the output, you can access the formatted string via the stringstream::str() function.

stringstreams are also handy for dealing with formatted input such as a CSV file. Use delimited form of getline() to read a line of data upto the '\n'. Create a stringstream from the line of data, then use getline again to get data from the line of data field by field, this time delimited by ','.

edit:
just explain exactly what a stream is / does in general?

This was also explained in your other thread.
http://www.cplusplus.com/forum/general/142052/#msg749868
Last edited on
What is formatted output?
Formatted output is where the stream formats data per specifications set for the stream.

Example:
1
2
3
 
  int a = 10;
  cout << dec << right << setfill('0') << setw(5) << a << endl;


Line 2 specifies setting decimal, right justified, zero fill, with a width of 5.

The alternative is unformatted (raw binary) output.
1
2
  int a = 10;
  os.write (&a, sizeof(a));

In this case, line 2 writes 4 binary bytes (assumes 32 bit int) to the output stream.
Last edited on
So you can't do:
cout << dec << right << setfill('0') << setw(5) << a << endl;
just to a string without using <sstream> and stringstream?
Of course you can, and I don't mean the http://www.cplusplus.com/reference/cstdio/snprintf/

All you have to do is to take each digit separately, convert to char, and append to string (after adding enough appropriate padding). But why reinvent a wheel, when std::ostringstream is fully functional?
Last edited on
I dont know what snprintf is, i have always just used <iostream> and nothing else.
That is good. When you know one stream, you practically know them all.
but i don't know what iostream does exactly either, i just learned that it has to be there so the code will run, hehe.
Topic archived. No new replies allowed.