打开文件 - 文件不存在但没有错误 [英] opening file - file does not exist but no error

查看:85
本文介绍了打开文件 - 文件不存在但没有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


这真让我疯狂。

我打开一个输入文件,我测试它是否成功打开。多数民众赞成

it!

我知道我试图打开的文件不存在,但我收到NO

错误信息。

无论我输入什么名字,我都会''文件成功打开'

任何线索?谢谢!


#include< iostream.h>

#include< stdlib.h>

#include< stdio.h>

#include< fstream.h>


int main()

{

ifstream infile;

char file_name [16];


printf(" \\\
Enter文件名:");

cin>> file_name;


infile.open(file_name);


if(infile.fail())

printf(" cant open%s",file_name);

else

printf(&\\; \ nfile%s已成功打开\ n,file_name);


infile.close();

返回0;

}

Hello,

This really drives me nuts.
Im opening an input file, and Im testing if it was open successfully. Thats
it!
I know that the file that Im trying to open DOES NOT exist but Im getting NO
error message.
No matter what name I enter I get ''file opened successfully"
Any clues? Thanks!

#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <fstream.h>

int main()
{
ifstream infile;
char file_name[16];

printf("\nEnter file name: ");
cin>>file_name;

infile.open(file_name);

if (infile.fail())
printf("cant open %s",file_name);
else
printf("\nfile %s opened succesfully\n",file_name);

infile.close();
return 0;
}

推荐答案

ben写道:
你好,

这真让我疯了。
我打开一个输入文件,我测试它是否成功打开。多数民众赞成吧!
我知道我试图打开的文件不存在,但我收到NO
错误信息。
无论我输入什么名字,我都会得到''档案成功打开
任何线索?谢谢!

#include< iostream.h>
#include< stdlib.h>
#include< stdio.h>
#include< fstream.h>

int main()
{ifstream infile;
char file_name [16];

printf(" \ n输入文件名:");
cin>> file_name;

infile.open(file_name);

if(infile.fail() )


Truy将此替换为

if(infile)printf(cant open%s,file_name);
else
printf(\ nfile%s打开成功\ nn,file_name;

infile.close();
返回0;
}
Hello,

This really drives me nuts.
Im opening an input file, and Im testing if it was open successfully. Thats
it!
I know that the file that Im trying to open DOES NOT exist but Im getting NO
error message.
No matter what name I enter I get ''file opened successfully"
Any clues? Thanks!

#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <fstream.h>

int main()
{
ifstream infile;
char file_name[16];

printf("\nEnter file name: ");
cin>>file_name;

infile.open(file_name);

if (infile.fail())
Truy replacing this for
if(infile) printf("cant open %s",file_name);
else
printf("\nfile %s opened succesfully\n",file_name);

infile.close();
return 0;
}





" Jorge Rivera" <乔***** @ rochester.rr.com>在消息中写道

news:nz ******************* @ twister.nyroc.rr.com ...

"Jorge Rivera" <jo*****@rochester.rr.com> wrote in message
news:nz*******************@twister.nyroc.rr.com...
ben写道:
你好,

这真让我疯狂。
我打开一个输入文件,我测试它是否成功打开。
多数民众赞成吧!
我知道我试图打开的文件不存在,但是我的b $ b收到错误消息。
无论我输入什么名字,我都会得到''档案成功打开
任何线索?谢谢!

#include< iostream.h>
#include< stdlib.h>
#include< stdio.h>
#include< fstream.h>
Hello,

This really drives me nuts.
Im opening an input file, and Im testing if it was open successfully. Thats it!
I know that the file that Im trying to open DOES NOT exist but Im getting NO error message.
No matter what name I enter I get ''file opened successfully"
Any clues? Thanks!

#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <fstream.h>




也可以尝试用标准替换上面的


#include< iostream> //没有.h

#include< stdlib.h>

#include< stdio.h>

#include< fstream> ; //没有.h

使用命名空间std;


取决于你的编译器,但你有更多的机会获得标准的

行为如果您使用标准头文件。


john



Also try replacing the above with the standard

#include <iostream> // no .h
#include <stdlib.h>
#include <stdio.h>
#include <fstream> // no .h
using namespace std;

Depends on your compiler but you have more chance of getting standard
behaviour if you use the standard header files.

john




" Jorge Rivera" <乔***** @ rochester.rr.com>在消息中写道

news:nz ******************* @ twister.nyroc.rr.com ...

"Jorge Rivera" <jo*****@rochester.rr.com> wrote in message
news:nz*******************@twister.nyroc.rr.com...
ben写道:
你好,

这真让我疯狂。
我打开一个输入文件,我测试它是否成功打开。
多数民众赞成吧!
我知道我试图打开的文件不存在,但是我的b $ b收到错误消息。
无论我输入什么名字,我都会得到''档案成功打开
任何线索?谢谢!

#include< iostream.h>
#include< stdlib.h>
#include< stdio.h>
#include< fstream.h>

int main()
{ifstream infile;
char file_name [16];

printf(" \ n输入文件名:");
cin>> file_name;

infile.open(file_name);

if(infile.fail() )
Hello,

This really drives me nuts.
Im opening an input file, and Im testing if it was open successfully. Thats it!
I know that the file that Im trying to open DOES NOT exist but Im getting NO error message.
No matter what name I enter I get ''file opened successfully"
Any clues? Thanks!

#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <fstream.h>

int main()
{
ifstream infile;
char file_name[16];

printf("\nEnter file name: ");
cin>>file_name;

infile.open(file_name);

if (infile.fail())



特里取代这个
if(infile)



Truy replacing this for
if(infile)




我离开了我的std工作但是根据在线参考
http:// www。 cplusplus.com/ref/iostrea...torvoidpt.html

这应该与fail()即badbit或failbit相同

另一种选择是检查!好()好的包括eofbit

在线参考没有什么可说的开放的具体行为

有趣的本身。

P.S.当你真正尝试使用它时,你会得到failbit吗?



I left my std at work but according to an online reference at
http://www.cplusplus.com/ref/iostrea...torvoidpt.html
This should be the same as fail() i.e. badbit or failbit
An alternative would be to check for !good() as good includes eofbit
The online ref had nothing to say about the specific behaviour of open which
is interesting in itself.
P.S. do you get failbit when you actually try to use it?

printf(" cant open%s",file_name);
else
printf(\ nfile%s打开成功\ nn,file_name;

infile.close();
返回0;
}
printf("cant open %s",file_name);
else
printf("\nfile %s opened succesfully\n",file_name);

infile.close();
return 0;
}



这篇关于打开文件 - 文件不存在但没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆