【发布时间】:2010-09-16 17:39:26
【问题描述】:
我有 ifstream 和一个在运行时可能打开或不打开的 ofstream(取决于用户在命令行中输入的内容。无论如何我都声明了变量,并且我有一个方法可以在需要时打开流。 我的问题是在程序结束时我不知道是否需要关闭它们。
C++ 中是否有任何方法可以知道是否打开了流?就像在 Java 中一样,您可以给流一个 null 值,然后询问它是否为 null(这意味着它从未打开过)..
是否可以关闭从未打开的流?
这是代码:
int main(int argc, char* argv[]) {
static std::ifstream ifs;
static std::ofstream ofs;
//might or might not open the streams:
OpenStreams(ifs,ofs,argc-1,argv);
........
//here i would like to close the streams at the end of the program
//or not (if they were not opened
return 0;
}
谢谢!
【问题讨论】:
标签: c++