检测文件是否打开 如何检测文件在C++中是否打开? 我正在尝试使用这样的代码: int main() { ifstream file("file.txt"); if ( /*here comes the check if file is open*/ ) cout<<"File open successfully"; else cout<<"File couldn't be opened. Check if the file is not used by another program or if it exists"; } intmain() { ifstream文件(“file.txt”); 如果(/*这里是检查文件是否打开*/)cout

检测文件是否打开 如何检测文件在C++中是否打开? 我正在尝试使用这样的代码: int main() { ifstream file("file.txt"); if ( /*here comes the check if file is open*/ ) cout<<"File open successfully"; else cout<<"File couldn't be opened. Check if the file is not used by another program or if it exists"; } intmain() { ifstream文件(“file.txt”); 如果(/*这里是检查文件是否打开*/)cout,c++,file,detection,file-exists,C++,File,Detection,File Exists,您正在查找函数is_open() 您正在调用ifstream::is_open()函数是否尝试了file.is_open()?您的意思是“if(file.is_open)…”?否我的意思是file.is_open(),可以通过is\u open成员函数来完成所需的文件是否打开检查。在某些情况下,您不需要显式检查文件是否打开,只需执行例如while(file>>某个变量){/*do something*/} if(file.is_open()){} if(file.is_open())

您正在查找函数is_open()


您正在调用
ifstream::is_open()
函数

是否尝试了
file.is_open()
?您的意思是“if(file.is_open)…”?否我的意思是
file.is_open()
,可以通过
is\u open
成员函数来完成所需的文件是否打开检查。在某些情况下,您不需要显式检查文件是否打开,只需执行例如
while(file>>某个变量){/*do something*/}
if(file.is_open()){}
if(file.is_open())