std::basic_istream::sync

来自cppreference.com
< cpp‎ | io‎ | basic istream

int sync();

同步输入缓冲区关联的数据源。

其行为就像UnformattedInputFunction,除了gcount()不会受到影响。在构造和检查岗哨对象的,

rdbuf()是一个空指针,返回-1

否则,呼叫rdbuf()->pubsync()。如果函数返回-1,要求setstate(badbit)和回报-1。否则,返回0.

目录

[编辑] 参数

(无)
原文:
(none)
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。

[编辑] 返回值

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';
}

可能的输出:

Reading from the file
abc
Reading from the file, with sync()
a23

[编辑] 另请参阅

[虚]
将缓冲与关联的字符序列同步
(std::basic_streambuf 的虚受保护成员函数) [编辑]
与底层存储设备同步
(std::basic_ostream 的公开成员函数) [编辑]