C Primier Plus IO Library

No Copy or Assign for IO Objects

Functions that do IO typically pass and return the stream through references. Reading or writing an IO object changes its state, so the reference must not be const.
The library lets us ignore the differences among these different kinds of streams by using inheritance
There are two other similar manipulators: flush and ends.
flush flushes the stream but adds no characters to the output; ends inserts a null character into the buffer and then flushes it

1
2
3
4
5
6
7
cout << "hi!" << endl; // writes hi and a newline, then flushes the buffer
cout << "hi!" << flush; // writes hi, then flushes the buffer; adds no data
cout << "hi!" << ends; // writes hi and a null, then flushes the buffer
cout << unitbuf; // all writes will be flushed immediately
// any output is flushed immediately, no buffering
cout << nounitbuf; // returns to normal buffering

Output buffers are not flushed if the program terminates abnormally.
Tying Input and Output Streams Together:
When an input stream is tied to an output stream, any attempt to read the input stream will first flush the buffer associated with the output stream.
To tie a given stream to a new output stream, we pass tie a pointer to the new stream. To untie the stream completely, we pass a null pointer. Each stream can be tied to at most one stream at a time. However, multiple streams can tie themselves to the same ostream

1
2
3
4
5
6
cin.tie(&cout); // illustration only: the library ties cin and cout for us
// old_tie points to the stream (if any) currently tied to cin
ostream *old_tie = cin.tie(nullptr); // cin is no longer tied
// ties cin and cerr; not a good idea because cin should be tied to cout
cin.tie(&cerr); // reading cin flushes cerr, not cout
cin.tie(old_tie); // reestablish normal tie between cin and cout

The only way to preserve the existing data in a file opened by an ofstream
is to specify app or in mode explicitly.
File Mode Is Determined Each Time open Is Called.