30 Input/output library [input.output]

30.9 File-based streams [file.streams]

30.9.2 Class template basic_­filebuf [filebuf] Member functions [filebuf.members]

bool is_open() const;

Returns: true if a previous call to open succeeded (returned a non-null value) and there has been no intervening call to close.

basic_filebuf* open(const char* s, ios_base::openmode mode); basic_filebuf* open(const filesystem::path::value_type* s, ios_base::openmode mode); // wide systems only; see [fstream.syn]

Effects: If is_­open() != false, returns a null pointer. Otherwise, initializes the filebuf as required. It then opens a file, if possible, whose name is the ntbs s (as if by calling fopen(s, modstr)). The ntbs modstr is determined from mode & ~ios_­base​::​ate as indicated in Table 117. If mode is not some combination of flags shown in the table then the open fails.

Table 117 — File open modes
ios_­base flag combination stdio equivalent
binary in out trunc app
+ "w"
+ + "a"
+ "a"
+ + "w"
+ "r"
+ + "r+"
+ + + "w+"
+ + + "a+"
+ + "a+"
+ + "wb"
+ + + "ab"
+ + "ab"
+ + + "wb"
+ + "rb"
+ + + "r+b"
+ + + + "w+b"
+ + + + "a+b"
+ + + "a+b"

If the open operation succeeds and (mode & ios_­base​::​ate) != 0, positions the file to the end (as if by calling fseek(file, 0, SEEK_­END)).327

If the repositioning operation fails, calls close() and returns a null pointer to indicate failure.

Returns: this if successful, a null pointer otherwise.

basic_filebuf* open(const string& s, ios_base::openmode mode); basic_filebuf* open(const filesystem::path& s, ios_base::openmode mode);

Returns: open(s.c_­str(), mode);

basic_filebuf* close();

Effects: If is_­open() == false, returns a null pointer. If a put area exists, calls overflow(traits​::​​eof()) to flush characters. If the last virtual member function called on *this (between underflow, overflow, seekoff, and seekpos) was overflow then calls a_­codecvt.unshift (possibly several times) to determine a termination sequence, inserts those characters and calls overflow(traits​::​​eof()) again. Finally, regardless of whether any of the preceding calls fails or throws an exception, the function closes the file (as if by calling fclose(file)). If any of the calls made by the function, including fclose, fails, close fails by returning a null pointer. If one of these calls throws an exception, the exception is caught and rethrown after closing the file.

Returns: this on success, a null pointer otherwise.

Postconditions: is_­open() == false.

The macro SEEK_­END is defined, and the function signatures fopen(const char*, const char*) and fseek(FILE*, long, int) are declared, in <cstdio>.