Namespaces
Variants
Actions

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

From cppreference.com
< cpp‎ | io‎ | basic filebuf
 
C++
 
Input/output library
 
std::basic_filebuf
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

#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
}

[edit] See also

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

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant