std::basic_filebuf<CharT,Traits>::swap
From cppreference.com
< cpp | io | basic filebuf
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Input/output library
Print functions (C++23)
Buffers
(C++23)
(C++98/26*)
(C++20)
Streams
Abstractions
File I/O
String I/O
Array I/O
(C++23)
(C++23)
(C++23)
(C++98/26*)
(C++98/26*)
(C++98/26*)
Synchronized Output
(C++20)
Types
Error category interface
(C++11)
(C++11)
std::basic_filebuf
Public member functions
(C++11)
basic_filebuf::swap
(C++11)
(C++26)
Protected member functions
Non-member functions
(C++11)
void swap( std::basic_filebuf & rhs );
(since C++11)
Swaps the state and the contents of *this and rhs.
Contents
[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 <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 }