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

From cppreference.com
< cpp‎ | io‎ | basic filebuf
 
 
Input/output library
I/O manipulators
C-style I/O
Buffers
(deprecated in C++98)
Streams
Abstractions
File I/O
String I/O
Array I/O
(deprecated in C++98)
(deprecated in C++98)
(deprecated in C++98)
Synchronized Output
Types
Error category interface
(C++11)
 
 
void swap( std::basic_filebuf& rhs );
(since C++11)

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

Parameters

rhs - another basic_filebuf

Return value

(none)

Notes

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

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
}


See also

(C++11)
assigns a basic_filebuf object
(public member function)
specializes the std::swap algorithm
(function template)
(C++11)
swaps two file streams
(public member function of std::basic_fstream<CharT,Traits>)