std::basic_istream<CharT,Traits>::sync
提供: cppreference.com
< cpp | io | basic istream
int sync(); |
||
入力バッファを紐付けられているデータソースと同期します。
UnformattedInputFunction として動作しますが、 gcount() は影響を受けません。 sentry オブジェクトの構築および確認の後、
rdbuf() がヌルポインタの場合は -1 を返します。
そうでなければ rdbuf()->pubsync() を呼びます。 この関数が -1 を返した場合は、 setstate(badbit) を呼び、 -1 を返します。 そうでなければ、 0 を返します。
目次 |
[編集] 引数
(なし)
[編集] 戻り値
成功した場合は 0、失敗した場合またはストリームがこの操作をサポートしない (バッファなし) 場合は -1。
[編集] ノート
readsome() と同様に、この関数がライブラリ提供のストリームに対して何かを行うかどうかは処理系定義です。 意図は一般的には、ストリームバッファが最後に get 領域を埋めた後、紐付けられている入力シーケンスに行われたかもしれない何らかの変更を拾い上げるための、次の読み込み操作のためです。 それを達するために、 sync()
は get 領域を空にするかもしれませんし、 get 領域を埋め直すかもしれませんし、何もしないかもしれません。 重要な例外な Visual Studio です。 Visual Studio では、標準入力ストリームに対して呼ばれたとき、この操作は未処理の入力を破棄します。
[編集] 例
ファイル入力を用いて入力ストリームの sync() の使用をデモンストレーションします (実装されているプラットフォームの場合)。
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'; }
出力例:
Reading from the file abc Reading from the file, with sync() a23
[編集] 関連項目
[仮想] |
バッファを紐付けられている文字シーケンスと同期します ( std::basic_streambuf<CharT,Traits> の仮想プロテクテッドメンバ関数)
|
ベースとなるストレージデバイスと同期します ( std::basic_ostream<CharT,Traits> のパブリックメンバ関数)
|