Namespaces
Variants
Views
Actions

std::basic_filebuf::swap

From cppreference.com
< cpp‎ | io‎ | basic filebuf
Revision as of 22:30, 2 November 2012 by P12bot (Talk | contribs)

Template:ddcl list begin <tr class="t-dcl ">

<td >
void swap( std::basic_filebuf& rhs )
</td>

<td class="t-dcl-nopad"> </td> <td > (since C++11) </td> </tr> Template:ddcl list end

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

Contents

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

Template:cpp/io/basic filebuf/dcl list operator=Template:cpp/io/basic filebuf/dcl list swap2Template:cpp/io/basic fstream/dcl list swap