命名空间
变体
操作

std::basic_filebuf<CharT,Traits>::swap

来自 cppreference.cn
< cpp‎ | io‎ | basic filebuf
 
 
 
 
void swap( std::basic_filebuf& rhs );
(since C++11)

交换 *thisrhs 的状态和内容。

目录

[edit] 参数

rhs - 另一个 basic_filebuf

[edit] 返回值

(无)

[edit] 注解

当交换 std::fstream 对象时,此函数会自动调用,因此极少需要直接调用它。

[edit] 示例

#include <fstream>
#include <iostream>
#include <string>
 
int main()
{
    std::ifstream fin("test.in"); // read-only
    std::ofstream fout("test.out"); // write-only
 
    std::string s;
    getline(fin, s);
    std::cout << s << '\n'; // outputs the first line of test.in
 
    fin.rdbuf()->swap(*fout.rdbuf()); //swap the underlying buffers
 
    getline(fin, s); // fails: cannot read from a write-only filebuf
    std::cout << s << '\n'; // prints empty line
}

[edit] 参见

(C++11)
赋值 basic_filebuf 对象
(公共成员函数) [edit]
特化 std::swap 算法
(函数模板) [edit]
(C++11)
交换两个文件流
(std::basic_fstream<CharT,Traits> 的公共成员函数) [edit]