std::basic_istream<CharT,Traits>::sync (3) - Linux Manuals
std::basic_istream<CharT,Traits>::sync: std::basic_istream<CharT,Traits>::sync
Command to display std::basic_istream<CharT,Traits>::sync
manual in Linux: $ man 3 std::basic_istream<CharT,Traits>::sync
NAME
std::basic_istream<CharT,Traits>::sync - std::basic_istream<CharT,Traits>::sync
Synopsis
int sync();
Synchronizes the input buffer with the associated data source.
Behaves as UnformattedInputFunction, except that gcount() is not affected. After constructing and checking the sentry object,
if rdbuf() 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 readsome(), 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, as implemented on some platforms.
// Run this code
#include <iostream>
#include <fstream>
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 file
abc
Reading from the file, with sync()
a23
See also
sync synchronizes the buffers with the associated character sequence
(virtual protected member function of std::basic_streambuf<CharT,Traits>)
[virtual]
synchronizes with the underlying storage device
flush (public member function of std::basic_ostream<CharT,Traits>)
Pages related to std::basic_istream<CharT,Traits>::sync
- std::basic_istream<CharT,Traits>::seekg (3) - std::basic_istream<CharT,Traits>::seekg
- std::basic_istream<CharT,Traits>::sentry (3) - std::basic_istream<CharT,Traits>::sentry
- std::basic_istream<CharT,Traits>::swap (3) - std::basic_istream<CharT,Traits>::swap
- std::basic_istream<CharT,Traits>::basic_istream (3) - std::basic_istream<CharT,Traits>::basic_istream
- std::basic_istream<CharT,Traits>::gcount (3) - std::basic_istream<CharT,Traits>::gcount
- std::basic_istream<CharT,Traits>::get (3) - std::basic_istream<CharT,Traits>::get
- std::basic_istream<CharT,Traits>::getline (3) - std::basic_istream<CharT,Traits>::getline
- std::basic_istream<CharT,Traits>::ignore (3) - std::basic_istream<CharT,Traits>::ignore
- std::basic_istream<CharT,Traits>::operator= (3) - std::basic_istream<CharT,Traits>::operator=
- std::basic_istream<CharT,Traits>::operator>> (3) - std::basic_istream<CharT,Traits>::operator>>
- std::basic_istream<CharT,Traits>::peek (3) - std::basic_istream<CharT,Traits>::peek
- std::basic_istream<CharT,Traits>::putback (3) - std::basic_istream<CharT,Traits>::putback
- std::basic_istream<CharT,Traits>::read (3) - std::basic_istream<CharT,Traits>::read
- std::basic_istream<CharT,Traits>::readsome (3) - std::basic_istream<CharT,Traits>::readsome
- std::basic_istream<CharT,Traits>::tellg (3) - std::basic_istream<CharT,Traits>::tellg
- std::basic_istream<CharT,Traits>::unget (3) - std::basic_istream<CharT,Traits>::unget
- std::basic_istream<CharT,Traits>::~basic_istream (3) - std::basic_istream<CharT,Traits>::~basic_istream
- std::basic_istream (3) - std::basic_istream
- std::basic_istream_sentry (3)
- std::basic_istringstream (3) - std::basic_istringstream