C++ Fstream Library - Swap Function
Description
It exchanges the values of the fstream objects x and y.
Declaration
Following is the declaration for fstream::swap.
C++11
template <class charT, class traits> void swap (basic_fstream<charT,traits>& x, basic_fstream<charT,traits>& y);
Parameters
x,y − basic_fstream objects of the same type (i.e., having both the same template parameters, charT and traits).
x,y − basic_fstream objects of the same type (i.e., having both the same template parameters, charT and traits).
Return Value
none
Exceptions
No-throw guarantee − this member function never throws exceptions.
Data races
Both objects, x and y, are modified.
Example
In below example explains about fstream swap function.
#include <fstream>
int main () {
std::fstream foo;
std::fstream bar ("test.txt");
swap(foo,bar);
foo << "tutorialspoint";
foo.close();
return 0;
}
fstream.htm