public member function
<fstream>

std::basic_filebuf::close

basic_filebuf* close();
Close file
Closes the file currently associated with the object, disassociating them.

Any pending output sequence is written to the file (as if calling virtual member overflow), along with any unshift characters (codecvt::unshift), when needed.

The function fails if any of these operations fail, or if no file is currently open.

Parameters

none

Return Value

The function returns this, if successful.
In case of failure (including when the file is not open), a null pointer is returned.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// filebuf::close()
#include <iostream>
#include <fstream>

int main () {
  std::ifstream is;
  std::filebuf * fb = is.rdbuf();

  fb->open ("test.txt",std::ios::in);

  // appending operations

  fb->close();

  return 0;
}

Data races

Modifies the basic_filebuf object.
Concurrent access to the same file stream buffer object may introduce data races.

Exception safety

Basic guarantee: if an exception is thrown, the file stream buffer is in a valid state.
Any exception thrown by an internal operation is caught by the function and rethrown after closing the file.

See also