Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

14.2 Explicit Synchronization

You can force the stream to empty its buffer into an output file, or to refill its buffer from an input file. This is done through the stream buffer's function pubsync(). Typically, you call pubsync() indirectly through functions of the stream layer. Input streams and output streams have different member functions that implicitly call pubsync().

14.2.1 Output Streams

Output streams have a flush() function that writes the buffer content to the file. In case of failure, badbit is set or an exception thrown, depending on the exception mask.

//1The attempt to extract anything from the file /tmp/fil after this insertion will probably fail, because the string "Hello" is buffered and not yet written to the external file.
//2After the call to flush(), however, the file contains "Hello World!\n". (Incidentally, the call to ostr.flush() can be replaced by the flush manipulator; that is, ostr << flush;)

Keep in mind that flushing is a time-consuming operation. The function flush() not only writes the buffer content to the file; it may also reread the buffer in order to maintain the current file position. For the sake of performance, you should avoid inadvertent flushing, as when the endl manipulator calls flush() on inserting the end-of-line character. (See Section 7.3.2.)

14.2.2 Input Streams

Input streams have a sync() function. It forces the stream to access the external device and refill its buffer, beginning with the current file position.17 The example below demonstrates the principle theoretically. In real life, however, the two streams would belong to two separate processes and could use the shared file to communicate.

//1Here the input stream extracts the first string from the shared file. In doing so, the input stream fills its buffer. It reads as many characters from the external file as needed to fill the internal buffer. For this reason, the number of characters to be extracted from the file is implementation-specific; it depends on the size of the internal stream buffer.
//2The output stream overwrites part of the file content. Now the file content and the content of the input stream's buffer are inconsistent. The file contains "Hello Peter!"; the input stream's buffer still contains "Hello World!".
//3This extraction takes the string "World!" from the buffer instead of yielding "Peter!", which is the current file content.
//4More characters are appended to the external file. The file now contains "Hello Peter! Happy Birthday!", whereas the input stream's buffer is still unchanged.
//5This extraction yields nothing. The input stream filled its buffer with the entire content of the file because the file is so small in our toy example. Subsequent extractions made the input stream hit the end of its buffer, which is regarded as the end of the file as well. The extraction results in eofbit set, and nothing is extracted. There is no reason to ever access the external file again.
//6A call to sync() eventually forces the input stream to refill the buffer from the external device, beginning with the current file position. After the synchronization, the input stream's buffer contains "Happy Birthday!\n". The next extraction yields "Happy".

As the standard specifies the behavior of sync() as implementation-defined, you can alternatively try repositioning the input stream to the current position instead; for example, istr.seekg(ios_base::cur);.

NOTE: If you must synchronize several streams that share a file, it is advisable to call the sync() function after each output operation and before each input operation.

Previous fileTop of DocumentContentsIndexNext file

OEM Edition, ©Copyright 1999, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.