C++ 可以重用 fstream 来打开和写入多个文件吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2284775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ can I reuse fstream to open and write multiple files?
提问by 5YrsLaterDBA
I have 10 files need to be open for write in sequence. Can I have one fstream to do this? Do I need to do anything special (except flush()) in between each file or just call open(file1, fstream::out | std::ofstream::app)
for a each file and close the stream at the end of all 10 files are written.
我有 10 个文件需要按顺序打开以进行写入。我可以有一个 fstream 来做到这一点吗?我是否需要在每个文件之间执行任何特殊操作(flush() 除外),还是只调用open(file1, fstream::out | std::ofstream::app)
每个文件并在所有 10 个文件写入结束时关闭流。
回答by GManNickG
You will need to close it first, because calling open
on an already open stream fails. (Which means the failbit
flag is set to true). Note close()
flushes, so you don't need to worry about that:
您需要先关闭它,因为调用open
已经打开的流会失败。(这意味着failbit
标志设置为真)。注意close()
刷新,因此您无需担心:
std::ofstream file("1");
// ...
file.close();
file.clear(); // clear flags
file.open("2");
// ...
// and so on
Also note, you don't need to close()
it the last time; the destructor does that for you (and therefore also flush()
's). This may make a nice utility function:
另请注意,您不需要close()
最后一次;析构函数为您执行此操作(因此也是flush()
's)。这可能是一个很好的效用函数:
template <typename Stream>
void reopen(Stream& pStream, const char * pFile,
std::ios_base::openmode pMode = ios_base::out)
{
pStream.close();
pStream.clear();
pStream.open(pFile, pMode);
}
And you get:
你会得到:
std::ofstream file("1");
// ...
reopen(file, "2")
// ...
// and so on
回答by Judge Maygarden
Yes, but you must to close the fstream each time before opening the next file with it.
是的,但每次打开下一个文件之前,您都必须关闭 fstream。
However, it's better to use a new scoped fstream object for each file access to take advantage of the constructor and destructor behaviors:
但是,最好为每个文件访问使用一个新的作用域 fstream 对象,以利用构造函数和析构函数的行为:
struct {
const char *filename;
void (*operation)(fstream&);
} filelist[] = {
{ "file1", callback1 },
{ "file2", callback2 },
...
{ "file10", callback10 },
};
for (int i = 0; i < 10; ++i) {
fstream f(filelist[i].filename);
filelist[i].operation(f);
}
In the code sample above, the fstream is flushed and closed each time through the for loop because the destructor is called when the object loses scope. The fstream is passed by reference to a callback functionso that the operations can be handled on a per file basis without a nasty switch statement. If the operation is the same for each file, then that construct can be eliminated.
在上面的代码示例中,每次通过 for 循环刷新和关闭 fstream,因为当对象失去作用域时会调用析构函数。fstream 通过引用传递给回调函数,以便可以在每个文件的基础上处理操作,而无需使用讨厌的 switch 语句。如果每个文件的操作都相同,则可以消除该构造。
回答by John Knoeller
Closing a stream implies flush(), so as long as you close()
each stream before you open the next one you should be fine.
关闭流意味着flush(),因此只要您close()
在打开下一个流之前打开下一个流就可以了。