std::basic_filebuf<CharT,Traits>::swap (3) - Linux Manuals

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


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


void swap( std::basic_filebuf& rhs ); (since C++11)

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


rhs - another basic_filebuf

Return value



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


// Run this code

  #include <fstream>
  #include <string>
  #include <iostream>

  int main()
      std::ifstream fin(""); // read-only
      std::ofstream fout("test.out"); // write-only

      std::string s;
      getline(fin, s);
      std::cout << s << '\n'; // outputs the first line of

      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

See also

operator= assigns a basic_filebuf object
                              (public member function)

std::swap(std::basic_filebuf) specializes the std::swap algorithm
                              (function template)

swap swaps two file streams
                              (public member function of std::basic_fstream<CharT,Traits>)