怎么用ifstream判断已经打开?

时间:2022-02-15 18:57:16
ifstream input("text.txt");
当用完该文件,关闭!

input.close();
input.clear();

可是我在执行上面的动作时,要判断input文件是否打开,可是我用input.is_open()总是返回0,不管文件是否已经打开??

如何判断ifstream对应的文件已经打开????

7 个解决方案

#1


if(!input.is_open)

#2


Determines if a file is open.

bool is_open( );
 
Return Value
true if the file is open, false otherwise.

#3


Determines if a file is open.

bool is_open( );
 
Return Value
true if the file is open, false otherwise.

#4


路径对吗?

#5


ifstream input("text.txt"); 
用input.is_open()判断 成功返回1 失败返回0;
你总返回0是因为文件打开失败!
你的方法没问题

还有一种方法
         ifstream input;
input.open("1.txt",ios::nocreate);
if(input.is_open())
cout<<"打开成功!"<<endl;
else
cout<<"打开失败!"<<endl;

#6


直接if(!input)不就可以了

#7


我有一个问题要问六楼,判断文件打开与否不是用if( !input.is_open())吗,怎么直接写if(!input)也行呀,我都调试了,都是正确的,这是为什么呀!!!
回答一下呀,麻烦了呀,或是会的人发到我的邮箱呀hanyinlong@126.com,
c++编程高手请加我一下QQ呀:397675153,有几个问题要问一下,谢了!!!! 

#1


if(!input.is_open)

#2


Determines if a file is open.

bool is_open( );
 
Return Value
true if the file is open, false otherwise.

#3


Determines if a file is open.

bool is_open( );
 
Return Value
true if the file is open, false otherwise.

#4


路径对吗?

#5


ifstream input("text.txt"); 
用input.is_open()判断 成功返回1 失败返回0;
你总返回0是因为文件打开失败!
你的方法没问题

还有一种方法
         ifstream input;
input.open("1.txt",ios::nocreate);
if(input.is_open())
cout<<"打开成功!"<<endl;
else
cout<<"打开失败!"<<endl;

#6


直接if(!input)不就可以了

#7


我有一个问题要问六楼,判断文件打开与否不是用if( !input.is_open())吗,怎么直接写if(!input)也行呀,我都调试了,都是正确的,这是为什么呀!!!
回答一下呀,麻烦了呀,或是会的人发到我的邮箱呀hanyinlong@126.com,
c++编程高手请加我一下QQ呀:397675153,有几个问题要问一下,谢了!!!!