std::basic_filebuf<CharT,Traits>:: swap
From cppreference.net
<
cpp
|
io
|
basic filebuf
|
void
swap
(
std::
basic_filebuf
&
rhs
)
;
|
(自 C++11 起) | |
交换 * this 与 rhs 的状态及内容。
目录 |
参数
| rhs | - |
另一个
basic_filebuf
|
返回值
(无)
注释
此函数在交换 std::fstream 对象时会自动调用,很少需要直接调用该函数。
示例
运行此代码
#include <fstream> #include <iostream> #include <string> int main() { std::ifstream fin("test.in"); // 只读模式 std::ofstream fout("test.out"); // 只写模式 std::string s; getline(fin, s); std::cout << s << '\n'; // 输出 test.in 的第一行内容 fin.rdbuf()->swap(*fout.rdbuf()); // 交换底层缓冲区 getline(fin, s); // 操作失败:无法从只写文件缓冲区读取数据 std::cout << s << '\n'; // 输出空行 }
参见
|
(C++11)
|
赋值
basic_filebuf
对象
(公开成员函数) |
|
(C++11)
|
特化
std::swap
算法
(函数模板) |
|
(C++11)
|
交换两个文件流
(
std::basic_fstream<CharT,Traits>
的公开成员函数)
|