C++ Fstream Library - Swap Function



Description

It is used to exchanges all internal data between x and *this.

Declaration

Following is the declaration for fstream::swap.

C++11

void swap (basic_fstream& x);

Parameters

x − Another basic_fstream object of the same type (i.e., with the same template parameters charT and traits).

Return Value

none

Exceptions

No-throw guarantee − this member function never throws exceptions.

Data races

It modifies both stream objects (*this and x).

Example

In below example explains about fstream swap function.

#include <fstream>

int main () {
   std::fstream foo;
   std::fstream bar ("test.txt");

   foo.swap(bar);

   foo << "lorem ipsum";

   foo.close();

   return 0;
}
fstream.htm
Advertisements