basic_filebuf
's relation to C FILE
semanticsSection: 31.10.3.5 [filebuf.virtuals] Status: C++17 Submitter: Aaron Ballman Opened: 2015-02-09 Last modified: 2017-07-30
Priority: 2
View all other issues in [filebuf.virtuals].
View all issues with C++17 status.
Discussion:
The restrictions on reading and writing a sequence controlled by an object of class
basic_filebuf<charT, traits>
are the same as for reading and writing with
the Standard C library FILE
s. One of the restrictions placed by C is on the behavior of
a stream that is opened for input and output. See the C99 standard, 7.19.5.3p6 for more
details, but the gist is that when opened in update mode, reads and writes must have an
intervening file positioning or flushing call to not trigger UB.
31.10.3.5 [filebuf.virtuals] p13 specifies that basic_filebuf::seekoff()
calls
std::fseek()
. However, there is no mention of std::fseek()
in
basic_filebuf::seekpos()
, and no mention of std::fflush()
in
basic_filebuf::sync()
, which seem like an oversight.
Previous resolution [SUPERSEDED]:
This wording is relative to N4296.
Change 31.10.3.5 [filebuf.virtuals] p16 as follows [Editorial note: A footnote referring to
fseek
is not needed, because this is already covered by the existing footnote 334]:-16- Alters the file position, if possible, to correspond to the position stored in
sp
(as described below). Altering the file position performs as follows:
if
(om & ios_base::out) != 0
, then update the output sequence and write any unshift sequence;set the file position to
sp
as if by callingstd::fseek(file, sp, SEEK_SET)
;if
(om & ios_base::in) != 0
, then update the input sequence;where
om
is the open mode passed to the last call toopen()
. The operation fails ifis_open()
returns false.Change 31.10.3.5 [filebuf.virtuals] p19 as follows and add a new footnote that mimics comparable footnotes in 31.10.3.4 [filebuf.members] and 31.10.3.5 [filebuf.virtuals]:
-19- Effects: If a put area exists, calls
filebuf::overflow
to write the characters to the file, then flushes the file as if by callingstd::fflush(file)
[Footnote: The function signaturefflush(FILE*)
is declared in<cstdio>
(27.9.2).]. If a get area exists, the effect is implementation-defined.
[2015-05, Lenexa]
Aaron provides improved wording by removing the params from std::fseek()
due to the concerns
regarding the parameters on systems where fseek
uses 32-bit parameters.
drops the std::
drops the footnote for fflush
replaces fseek
with fsetpos
Previous resolution [SUPERSEDED]:
This wording is relative to N4431.
Change 31.10.3.5 [filebuf.virtuals] p16 as follows [Editorial note: A footnote referring to
fseek
is not needed, because this is already covered by the existing footnote 334]:-16- Alters the file position, if possible, to correspond to the position stored in
sp
(as described below). Altering the file position performs as follows:
if
(om & ios_base::out) != 0
, then update the output sequence and write any unshift sequence;set the file position to
sp
as if by a call tostd::fseek
;if
(om & ios_base::in) != 0
, then update the input sequence;where
om
is the open mode passed to the last call toopen()
. The operation fails ifis_open()
returns false.Change 31.10.3.5 [filebuf.virtuals] p19 as follows and add a new footnote that mimics comparable footnotes in 31.10.3.4 [filebuf.members] and 31.10.3.5 [filebuf.virtuals]:
-19- Effects: If a put area exists, calls
filebuf::overflow
to write the characters to the file, then flushes the file as if by callingstd::fflush(file)
[Footnote: The function signaturefflush(FILE*)
is declared in<cstdio>
(27.9.2).]. If a get area exists, the effect is implementation-defined.
Proposed resolution:
This wording is relative to N4431.
Change 31.10.3.5 [filebuf.virtuals] p16 as follows:
-16- Alters the file position, if possible, to correspond to the position stored in
sp
(as described below). Altering the file position performs as follows:
if
(om & ios_base::out) != 0
, then update the output sequence and write any unshift sequence;set the file position to
sp
as if by a call tofsetpos
;if
(om & ios_base::in) != 0
, then update the input sequence;where
om
is the open mode passed to the last call toopen()
. The operation fails ifis_open()
returns false.
Change 31.10.3.5 [filebuf.virtuals] p19 as follows:
-19- Effects: If a put area exists, calls
filebuf::overflow
to write the characters to the file, then flushes the file as if by callingfflush(file)
. If a get area exists, the effect is implementation-defined.