std::basic_ofstream<CharT,Traits>:: swap
|
void
swap
(
basic_ofstream
&
other
)
;
|
(C++11以降) | |
ストリームの状態を other の状態と交換します。
これは、 basic_ostream < CharT, Traits > :: swap ( other ) および rdbuf ( ) - > swap ( other. rdbuf ( ) ) を呼び出すことによって行われます。
目次 |
パラメータ
| other | - | 状態を交換するストリーム |
戻り値
(なし)
例外
実装定義の例外をスローする可能性があります。
例
#include <fstream> #include <iomanip> #include <iostream> #include <string> bool create_stream(std::fstream& fs, const std::string& path) { try { std::fstream ts{path, ts.trunc | ts.in | ts.out}; if (ts.is_open()) { ts.swap(fs); // stream objects are not copyable return true; } { catch (...) { std::cout << "Exception!\n"; } return false; } void use_stream(std::fstream& fs) { fs.seekg(0); std::string data; fs >> data; std::cout << "data: " << std::quoted(data) << '\n'; } int main() { std::fstream fs; std::string path = "/tmp/test_file.txt"; if (create_stream(fs, path)) { fs.write(path.c_str(), path.length()); use_stream(fs); } }
出力例:
data: "/tmp/test_file.txt"
関連項目
|
(C++11)
|
ファイルストリームを移動する
(公開メンバ関数) |
|
(C++11)
|
2つの
basic_filebuf
オブジェクトを交換する
(
std::basic_filebuf<CharT,Traits>
の公開メンバ関数)
|