默认情况下,std :: ofstream是否截断或追加? [英] Does std::ofstream truncate or append by default?
问题描述
如果您调用 std :: ofstream
构造函数而没有 openmode
标志,则默认标志为 ios_base :: out
。但这是否意味着 ios_base :: trunc
或 ios_base :: app
?
If you call the std::ofstream
constructor without openmode
flags, the default flag is ios_base::out
. But does this imply ios_base::trunc
or ios_base::app
?
换句话说,如果您的文件系统中已经有一个非空文件 past.txt,并且您调用了
In other words, if you already have a non-empty file "past.txt" in your file system and you call
std::ofstream stream( "past.txt" );
stream << "new content";
将新内容附加到 past.txt的先前内容中,或者替换为
will "new content" be appended to the prior contents of "past.txt" or will it replace the prior contents?
推荐答案
简短版本
它会被截断
The short version
It truncates by default.
该标准基本上是意大利面,但最终归结为说 fopen(const char *, w)
(27.9.1.4 [filebuf.members]) ,然后将我们引向ISO C 7.9标准。
The standard is basically spaghetti on this, but it eventually boils down to saying that it's the equivalent of saying fopen(const char*, "w")
(27.9.1.4 [filebuf.members]), which then points us towards the ISO C 7.9 standard.
检查是否提供了第7.19.5.3节 fopen函数,它指定了传递 w时的行为:
Checking that out provides us with §7.19.5.3, "The fopen function", which specifies the behavior when "w" is passed:
w截短为零长度或创建用于写入的文本文件
w truncate to zero length or create text file for writing
长版本
如果您想自己跟踪意大利面条,请从27.9.1.11 [ofstream.cons]开始,它将构造函数的行为描述为
The long version
If you'd like to follow the spaghetti trail yourself, start with 27.9.1.11 [ofstream.cons], which describes the constructor's behavior as
效果:构造
class basic_ofstream< charT,traits>
类的对象,初始化
basic_ostream(& sb
)的基本类,并用basic_filebuf<
(27.7.3.2,27.9.1.2),sb
初始化; charT,traits>())
然后调用rdbuf()-> open(s,mode | ios_base :: out)
。如果该函数返回空指针,则调用
setstate(failbit)
。
Effects: Constructs an object of
class basic_ofstream<charT,traits>
, initializing the base class withbasic_ostream(&sb
) and initializingsb
withbasic_filebuf<charT,traits>())
(27.7.3.2, 27.9.1.2), then callsrdbuf()->open(s, mode|ios_base::out)
. If that function returns a null pointer, callssetstate(failbit)
.
rdbuf()
返回 basic_filebuf< charT,traits> *
的地方(27.9.1.13 [ofstream])
Where rdbuf()
returns basic_filebuf<charT,traits>*
(27.9.1.13 [ofstream])
这将我们带到27.9.1.1 [filebuf],或更具体地讲是27.9.1.4 [filebuf.members],它描述了 open
函数:
Which leads us to 27.9.1.1 [filebuf], or more specifically, 27.9.1.4 [filebuf.members] , which describes the open
function:
basic_filebuf<charT,traits>* open(const char* s, ios_base::openmode mode);
as
效果:如果
is_open()!= false
,则返回空指针。否则,根据需要初始化filebuf。
然后,如果可能,它将打开一个文件,其名称为NTBSs
(就像通过调用std :: fopen(s, modstr)
)。
NTBSmodstr
是根据mode& 〜ios_base :: ate
如表132中所示。如果mode是
而不是表中显示的标志的某种组合,则打开失败。
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 NTBSs
(as if by callingstd::fopen(s,modstr)
). The NTBSmodstr
is determined frommode & ~ios_base::ate
as indicated in Table 132. If mode is not some combination of flags shown in the table then the open fails.
NTBS:空终止的字节串
NTBS: Null-terminated byte-string
表132描述了C ++ ios_base之间的等效规则: :openmode
和C样式的stdio字符串:
Table 132 describes equivalence rules between C++ ios_base::openmode
and C-style stdio strings:
Table 132 — File open modes
|
| 'ios_base' flag combination | 'stdio' equivalent |
| binary | in | out | trunc | app | |
| | | + | | | "w" |
| etc... |
在同一页上的脚注指出:
Which leads us to a footnote on the same page that states:
...函数签名
fopen(const char *,const char *)
和fseek(FILE *,long,
在
int)< cstdio>
( 27.9.2)。
...the function signatures
fopen(const char*, const char*)
andfseek(FILE*, long, int)
are declared, in<cstdio>
(27.9.2).
可以预见的是,这发送给我们27.9.2 [c.files],它提供了几乎无用的表134,但随后引用了C标准:
Which sends us, predictably, to 27.9.2 [c.files], which provides the nearly useless Table 134, but then references the C standard:
另请参见:ISO C 7.9,修订1 4.6.2。
See also: ISO C 7.9, Amendment 1 4.6.2.
我在此答案的主要部分中谈论过。
Which I talk about in the main portion of this answer.
这篇关于默认情况下,std :: ofstream是否截断或追加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!