C++ Fstream Library - Close Function


Advertisements

Description

Closes the file currently associated with the object, disassociating it from the stream. If the stream is currently not associated with any file (i.e., no file has successfully been open with it), calling this function fails.

Declaration

Following is the declaration for fstream::close.

void close();

Return Value

none

If the operation fails (including if no file was open before the call), the failbit state flag is set for the stream (which may throw ios_base::failure if that state flag was registered using member exceptions).

Exceptions

Basic guarantee − if an exception is thrown, the stream is in a valid state.

Any exception thrown by an internal operation is caught by the function and rethrown after closing the file.

It throws an exception of member type failure if the function fails (setting the failbit state flag) and member exceptions was set to throw for that state.

Data races

Modifies the fstream object.

Concurrent access to the same stream may introduce data races.

Example

In below example explains about fstream close function.

#include <fstream>      

int main () {

   std::fstream fs;
   fs.open ("test.txt", std::fstream::in | std::fstream::out | std::fstream::app);

   fs << " more lorem ipsum";

   fs.close();

   return 0;
}
fstream.htm
Advertisements