std::basic_filebuf::swap - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::basic_filebuf::swap

From cppreference.com
< cpp‎ | io‎ | basic filebuf

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

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

Run this code

    #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

operator= (C++11) | assigns a basic_filebuf object (public member function) [edit]
std::swap(std::basic_filebuf) (C++11) | specializes the std::swap algorithm (function template) [edit]
swap (C++11) | swaps two file streams (public member function of `std::basic_fstream`) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/io/basic_filebuf/swap&oldid=62726"