2015-06-14 128 views
0

如何检测文件是否以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"; 
    } 
+0

你尝试'file.is_open()'? – 101010

+0

你的意思是'if(file.is_open)...'吗? – Laurcons

+0

不,我的意思是'file.is_open()',[std :: istream :: is_open()](http://www.cplusplus.com/reference/fstream/ifstream/is_open/) – 101010

回答

0

您正在寻找的功能IS_OPEN()

if(file.is_open()){} 
0
if(file.is_open()) 

要调用ifstream::is_open()功能