2015-06-14 231 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()功能