I/O manipulators | ||||
Print functions(C++23) | ||||
C-style I/O | ||||
Buffers | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++20) | ||||
Streams | ||||
Abstractions | ||||
File I/O | ||||
String I/O | ||||
Array I/O | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
Synchronized Output | ||||
(C++20) | ||||
Types | ||||
Error category interface | ||||
(C++11) | ||||
(C++11) |
Global objects | ||||
Member functions | ||||
(C++11) | ||||
Formatted input | ||||
Unformatted input | ||||
Positioning | ||||
Miscellaneous | ||||
basic_istream::sync | ||||
(C++11) | ||||
Member classes | ||||
Non-member functions | ||||
int sync(); | ||
Synchronizes the input buffer with the associated data source.
Behaves asUnformattedInputFunction, except thatgcount() is not affected. After constructing and checking the sentry object,
Ifrdbuf() is a null pointer, returns-1.
Otherwise, callsrdbuf()->pubsync(). If that function returns-1, callssetstate(badbit) and returns-1. Otherwise, returns0.
Contents |
(none)
0 on success,-1 on failure or if the stream does not support this operation (is unbuffered).
As withreadsome(), 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.
Demonstrates the use of input streamsync()
with file input. Note that output here is implementation-defined, since calls tostd::basic_filebuf::sync are implementation-defined for reads.
#include <fstream>#include <iostream> void file_abc(){std::ofstream f("test.txt"); f<<"abc\n";} void file_123(){std::ofstream f("test.txt"); f<<"123\n";} int main(){ file_abc();// file now contains "abc"std::ifstream f("test.txt");std::cout<<"Reading from the file\n";char c; 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 fileabcReading from the file, with sync()a23
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 62 | C++98 | sync() returnedtraits::eof() ifrdbuf()->pubsync() returns-1 | returns-1 in this case |
[virtual] | synchronizes the buffers with the associated character sequence (virtual protected member function of std::basic_streambuf<CharT,Traits> )[edit] |
synchronizes with the underlying storage device (public member function of std::basic_ostream<CharT,Traits> )[edit] |