std::basic_filebuf<CharT,Traits>::swap - cppreference.com (original) (raw)

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

[edit] Parameters

rhs - another basic_filebuf

[edit] Return value

(none)

[edit] Notes

This function is called automatically when swapping std::fstream objects, it is rarely necessary to call it directly.

[edit] Example

#include #include #include   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