Files
2025-10-25 03:02:53 +03:00

175 lines
7.2 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[filebuf.members]
# 31 Input/output library [[input.output]](./#input.output)
## 31.10 File-based streams [[file.streams]](file.streams#filebuf.members)
### 31.10.3 Class template basic_filebuf [[filebuf]](filebuf#members)
#### 31.10.3.4 Member functions [filebuf.members]
[🔗](#lib:is_open,basic_filebuf)
`bool is_open() const;
`
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L11209)
*Returns*: true if a previous call toopen succeeded (returned a non-null value) and there has been no intervening
call to close[.](#1.sentence-1)
[🔗](#lib:open,basic_filebuf)
`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]](fstream.syn "31.10.1Header <fstream> synopsis")
`
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L11226)
*Preconditions*: s points to an NTCTS ([[defns.ntcts]](defns.ntcts "3.36NTCTS"))[.](#2.sentence-1)
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L11230)
*Effects*: Ifis_open() != false,
returns a null pointer[.](#3.sentence-1)
Otherwise,
initializes thefilebuf as required[.](#3.sentence-2)
It then opens
the file to which s resolves, if possible,
as if by a call to fopenwith the second argument determined frommode & ~ios_base::ate as indicated in Table [146](#tab:filebuf.open.modes "Table 146: File open modes")[.](#3.sentence-3)
If mode is not some combination of flags shown in the table then
the open fails[.](#3.sentence-4)
Table [146](#tab:filebuf.open.modes) — File open modes [[tab:filebuf.open.modes]](./tab:filebuf.open.modes)
| [🔗](#tab:filebuf.open.modes-row-1)<br>ios_base flag combination | | | | | | stdio equivalent |
| --- | --- | --- | --- | --- | --- | --- |
| [🔗](#tab:filebuf.open.modes-row-2)<br>binary | in | out | trunc | app | noreplace |
| [🔗](#tab:filebuf.open.modes-row-3) | | + | | | | "w" |
| [🔗](#tab:filebuf.open.modes-row-4) | | + | | | + | "wx" |
| [🔗](#tab:filebuf.open.modes-row-5) | | + | + | | | "w" |
| [🔗](#tab:filebuf.open.modes-row-6) | | + | + | | + | "wx" |
| [🔗](#tab:filebuf.open.modes-row-7) | | + | | + | | "a" |
| [🔗](#tab:filebuf.open.modes-row-8) | | | | + | | "a" |
| [🔗](#tab:filebuf.open.modes-row-9) | + | | | | | "r" |
| [🔗](#tab:filebuf.open.modes-row-10) | + | + | | | | "r+" |
| [🔗](#tab:filebuf.open.modes-row-11) | + | + | + | | | "w+" |
| [🔗](#tab:filebuf.open.modes-row-12) | + | + | + | | + | "w+x" |
| [🔗](#tab:filebuf.open.modes-row-13) | + | + | | + | | "a+" |
| [🔗](#tab:filebuf.open.modes-row-14) | + | | | + | | "a+" |
| [🔗](#tab:filebuf.open.modes-row-15)<br>+ | | + | | | | "wb" |
| [🔗](#tab:filebuf.open.modes-row-16)<br>+ | | + | | | + | "wbx" |
| [🔗](#tab:filebuf.open.modes-row-17)<br>+ | | + | + | | | "wb" |
| [🔗](#tab:filebuf.open.modes-row-18)<br>+ | | + | + | | + | "wbx" |
| [🔗](#tab:filebuf.open.modes-row-19)<br>+ | | + | | + | | "ab" |
| [🔗](#tab:filebuf.open.modes-row-20)<br>+ | | | | + | | "ab" |
| [🔗](#tab:filebuf.open.modes-row-21)<br>+ | + | | | | | "rb" |
| [🔗](#tab:filebuf.open.modes-row-22)<br>+ | + | + | | | | "r+b" |
| [🔗](#tab:filebuf.open.modes-row-23)<br>+ | + | + | + | | | "w+b" |
| [🔗](#tab:filebuf.open.modes-row-24)<br>+ | + | + | + | | + | "w+bx" |
| [🔗](#tab:filebuf.open.modes-row-25)<br>+ | + | + | | + | | "a+b" |
| [🔗](#tab:filebuf.open.modes-row-26)<br>+ | + | | | + | | "a+b" |
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L11280)
If the open operation succeeds andios_base::ate is set in mode,
positions the file to the end
(as if by calling fseek(file, 0, SEEK_END), wherefile is the pointer returned by calling fopen)[.](#4.sentence-1)[292](#footnote-292 "The macro SEEK_­END is defined, and the function signatures fopen(const char*, const char*) and fseek(FILE*, long, int) are declared, in <cstdio>.")
[5](#5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L11297)
If the repositioning operation fails, callsclose() and returns a null pointer to indicate failure[.](#5.sentence-1)
[6](#6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L11302)
*Returns*: this if successful, a null pointer otherwise[.](#6.sentence-1)
[🔗](#lib:open,basic_filebuf_)
`basic_filebuf* open(const string& s, ios_base::openmode mode);
basic_filebuf* open(const filesystem::path& s, ios_base::openmode mode);
`
[7](#7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L11315)
*Returns*: open(s.c_str(), mode);
[🔗](#lib:close,basic_filebuf)
`basic_filebuf* close();
`
[8](#8)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L11326)
*Effects*: Ifis_open() == false,
returns a null pointer[.](#8.sentence-1)
If a put area exists, callsoverflow(traits::eof()) to flush characters[.](#8.sentence-2)
If the last virtual member function called on*this (betweenunderflow,overflow,seekoff,
andseekpos)
wasoverflow then callsa_codecvt.unshift (possibly several times) to determine a termination sequence, inserts those
characters and callsoverflow(traits::eof()) again[.](#8.sentence-3)
Finally, regardless of whether any of the preceding calls fails or throws an
exception, the function closes the file
(as if by callingfclose(file))[.](#8.sentence-4)
If any of the calls made by the function, including fclose, fails,close fails by returning a null pointer[.](#8.sentence-5)
If one of these calls throws an
exception, the exception is caught and rethrown after closing the file[.](#8.sentence-6)
[9](#9)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L11359)
*Postconditions*: is_open() == false[.](#9.sentence-1)
[10](#10)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L11363)
*Returns*: this on success, a null pointer otherwise[.](#10.sentence-1)
[🔗](#lib:native_handle,basic_filebuf)
`native_handle_type native_handle() const noexcept;
`
[11](#11)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L11375)
*Preconditions*: is_open() is true[.](#11.sentence-1)
[12](#12)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L11379)
*Returns*: The native handle associated with *this[.](#12.sentence-1)
[292)](#footnote-292)[292)](#footnoteref-292)
The macro SEEK_END is defined, and the function signaturesfopen(const char*, const char*) andfseek(FILE*, long, int)are declared, in [<cstdio>](cstdio.syn#header:%3ccstdio%3e "31.13.1Header <cstdio> synopsis[cstdio.syn]")[.](#footnote-292.sentence-1)