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

> C++ Atlas: `cppatlas.com/w/cpp/io/basic_filebuf/swap` | Source: https://en.cppreference.com/w/cpp/io/basic_filebuf/swap | Domain: Input/output | Standards: C++11

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

## Official Specification Summary (cppreference)

```cpp
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

```cpp
#include <fstream>
#include <iostream>
#include <string>

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

- [`operator=`](https://en.cppreference.com/w/cpp/io/basic_filebuf/operator=) (C++11): assigns a `basic_filebuf` object (public member function)
- [`std::swap(std::basic_filebuf)`](https://en.cppreference.com/w/cpp/io/basic_filebuf/swap2) (C++11): specializes the `std::swap` algorithm (function template)
- [`swap`](https://en.cppreference.com/w/cpp/io/basic_fstream/swap) (C++11): swaps two file streams (public member function of `std::basic_fstream<CharT,Traits>`)

---
Attribution: content from https://en.cppreference.com/w/cpp/io/basic_filebuf/swap (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.