Namespaces
Variants

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

From cppreference.net
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); // 失敗: 書き込み専用filebufから読み取れない
    std::cout << s << '\n'; // 空行を表示
}

関連項目

(C++11)
basic_filebuf オブジェクトを代入する
(公開メンバ関数)
std::swap アルゴリズムを特殊化する
(関数テンプレート)
(C++11)
2つのファイルストリームを交換する
( std::basic_fstream<CharT,Traits> の公開メンバ関数)