std::basic_istream::sync
来自cppreference.com
< cpp | io | basic istream
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
int sync(); |
||
同步输入缓冲区关联的数据源.
原文:
Synchronizes the input buffer with the associated data source.
的行为就像
UnformattedInputFunction
,除了gcount()
不会受到影响。在构造和检查岗哨对象的,原文:
Behaves as
UnformattedInputFunction
, except that gcount()
is not affected. After constructing and checking the sentry object,否则,呼叫rdbuf()->pubsync()。如果函数返回-1,要求setstate(badbit)和回报-1。否则,返回0.
原文:
Otherwise, calls rdbuf()->pubsync(). If that function returns -1, calls setstate(badbit) and returns -1. Otherwise, returns 0.
目录 |
[编辑] 参数
(无)
[编辑] 返回值
0成功,-1故障,或如果流不支持此操作(无缓冲).
原文:
0 on success, -1 on failure or if the stream does not support this operation (is unbuffered).
[编辑] 注释
与
readsome()
,这是实现定义这个函数是否与图书馆提供的流做任何事情。这样做的目的是通常用于拿起任何变化,可能已作出的相关联的输入序列的下一个读操作后的数据流缓存器上次填充其get区域。为了实现这一目标,同步(),可以清空获取的区域,否则可能填充它,或者它可能什么也不做。一个值得注意的例外是Visual Studio中,此操作时丢弃未处理的输入调用标准输入流.原文:
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.[编辑] 示例
说明在某些平台上实现文件的输入,使用输入流同步().
原文:
Demonstrates the use of input stream sync() with file input, as implemented on some platforms.
#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
[编辑] 另请参阅
[虚]</div></div>
|
同步缓冲区关联的字符序列 原文: synchronizes the buffers with the associated character sequence (虚拟保护成员函数of std::basic_streambuf )
|
与底层存储设备同步 原文: synchronizes with the underlying storage device (公共成员函数of std::basic_ostream )
|