std::of流是否保证在打开新文件时会关闭旧的打开文件


#include <fstream>
int main()
{
auto fout = std::ofstream("/tmp/a.txt");
fout.open("/tmp/b.txt"); // Will "/tmp/a.txt" be closed?
fout.open("/tmp/c.txt"); // Will "/tmp/b.txt" be closed?
}

std::ofstream是否保证在打开新文件时会关闭旧的打开文件

第二次和后续调用将失败。

[filebuf.members]
basic_filebuf* open(const char* s, ios_base::openmode mode);
2效果:如果is_open() != false,则返回空指针。否则

[流成员]
void open(const char* s, ios_base::openmode mode = ios_base::out);
3效果:调用rdbuf()->open(s, mode | ios_base::out)。如果该函数没有返回空指针,则调用clear(),否则调用setstate(failbit)

最新更新