std::basic_filebuf<CharT,Traits>::swap
来自cppreference.com
                    
                                        
                    < cpp | io | basic filebuf
                    
                                                            
                    |   void swap( std::basic_filebuf& rhs );  | 
(C++11 起) | |
交换 *this 与 rhs 的状态和内容。
参数
| rhs | - |  另一 basic_filebuf
 | 
返回值
(无)
注意
在交换 std::fstream 对象时自动调用此函数,很少需要直接调用它。
示例
运行此代码
#include <fstream> #include <string> #include <iostream> 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); // 失败:不能从只写 filebuf 读取 std::cout << s << '\n'; // 打印空行 }
参阅
|    (C++11)  | 
  赋值 basic_filebuf 对象 (公开成员函数)  | 
|    特化 std::swap 算法  (函数模板)  | |
|    (C++11)  | 
  交换两个文件流  ( std::basic_fstream<CharT,Traits> 的公开成员函数)  |