Previous fileTop of DocumentContentsIndex pageNext file
Apache C++ Standard Library User's Guide

35.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 public member 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().

35.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 std::flush manipulator; that is, ostr << std::flush;)

Keep in mind that flushing is a time-consuming operation. The member 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 std::endl manipulator calls flush() on inserting the end-of-line character. (See Section 28.3.2.)

35.2.2 Input Streams

Input streams define the sync() member function. It forces the stream to access the external device and refill its buffer, beginning with the current file position. In the case of input streams, the behavior of sync() is implementation-defined, that is, not standardized. The traditional iostreams had a sync() function that did the expected synchronization, that is, refilling the buffer beginning with the current file position.

The example below demonstrates the principle theoretically. In real life, however, the two streams might belong to two separate processes. (For example, if two processes communcate through a shared file.) It should be noted that the exact behavior of the example below depends on the size of the internal buffers and other inherently implementation-specific parameters.

//1Here the input stream extracts the first substring, "Hello", 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. We will assume for the remainder of this discussion that the entire external sequence has been read into the internal 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 contents of the input stream's buffer are unchanged and still contain "Hello World!".
//3This extraction takes the string "World!" from the buffer instead of yielding "Peter!", which is the current file content. Since the file ends with a new line, the extractor terminates as soon as it encounters '\n' without setting eofbit. Had the the file not been new line terminated the extractor would have set eofbit.
//4More characters are appended to the internal buffer and subsequently flushed to the the external file. The file now contains "Hello Peter! Happy Birthday!". The input stream's buffer has not changed.
//5This extraction yields "Happy". The stream extractor first extracts the new line character that terminated input in 2. Since the default behavior is to skip all whitespace, the function proceeds extracting subsequent characters from the buffer. However, since the new line was the last character available in the internal buffer, the latter underflows and is refilled from the external sequence. The string "Happy Birthday!\n" is read in from the file and the first space-delimited substring is stored in s.
//6A call to sync() eventually forces the input stream to refill the buffer from the external device. The buffer tries to maintain the current position within the external device. After the synchronization, the input stream's buffer contains at the very least the initial substring " Birthday!\n", and the next extraction yields "Birthday!".

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(std::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 DocumentContentsIndex pageNext file