Namespaces
Variants
Views
Actions

std::basic_filebuf::swap

From cppreference.com
void swap( std::basic_filebuf& rhs )
(since C++11)

Swaps the state and the contents of *this and rhs.

Contents

[edit] Parameters

rhs - another basic_filebuf

[edit] Return value

(none)

[edit] Example

#include <fstream>
#include <string>
#include <iostream>
 
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] See also

(C++11)
assigns a basic_filebuf object
(public member function) [edit]
specializes the std::swap algorithm
(function template) [edit]