From cppreference.com
intsync();Synchronizes the input buffer with the associated data source.
Behaves as
, except that
is not affected. After constructing and checking the sentry object,
If
is a null pointer, returns -1.
Otherwise, calls rdbuf()->pubsync(). If that function returns -1, calls setstate(badbit) and returns -1. Otherwise, returns 0.
Parameters
(none)
Return value
0 on success, -1 on failure or if the stream does not support this operation (is unbuffered).
Notes
As with
, it is implementation-defined whether this function does anything with library-supplied streams. The intent is typically for the next read operation to pick up any changes that may have been made to the associated input sequence after the stream buffer last filled its get area. To achieve that, sync() may empty the get area, or it may refill it, or it may do nothing. A notable exception is Visual Studio, where this operation discards the unprocessed input when called with a standard input stream.
Example
Demonstrates the use of input stream sync() with file input. Note that output here is implementation-defined, since calls to
are implementation-defined for reads.
Run this code
#include<fstream>#include<iostream>voidfile_abc(){std::ofstreamf("test.txt");f<<"abc\n";}voidfile_123(){std::ofstreamf("test.txt");f<<"123\n";}intmain(){file_abc();// file now contains "abc"std::ifstreamf("test.txt");std::cout<<"Reading from the file\n";charc;f>>c;std::cout<<c;file_123();// file now contains "123"f>>c;std::cout<<c;f>>c;std::cout<<c<<'\n';f.close();file_abc();// file now contains "abc"f.open("test.txt");std::cout<<"Reading from the file, with sync()\n";f>>c;std::cout<<c;file_123();// file now contains "123"f.sync();f>>c;std::cout<<c;f>>c;std::cout<<c<<'\n';}Possible output:
Reading from the file abc Reading from the file, with sync() a23 Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
C++98 sync() returned traits::eof() if rdbuf()->pubsync() returns -1returns -1 in this case See also
[virtual]
synchronizes the buffers with the associated character sequence
(virtual protected member function of std::basic_streambuf<CharT,Traits>)
synchronizes with the underlying storage device
(public member function of std::basic_ostream<CharT,Traits>)