C++ 当中怎样测试文件打开是否成功,很郁闷啊,照书上写的编译器也报错

sysabod 2008-12-08 07:55:31
请看一下程序:
ifstream infile;
if(infile.open("test.dat",ios::in)==0) cout<<"open error"<<endl;
在这里省略了一些常规的东西,然后编译器居然报错,错误信息是
366 C:\Dev-Cpp\menu.cpp void value not ignored as it ought to be //这个是dev c++的
error C2120: 'void' illegal with all types //这个是vs2008的。
大侠救救我
...全文
180 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
Qlaiaqu 2008-12-13
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 sysabod 的回复:]
大家好,可是我的书是说这样检验的(谭浩强的C++程序设计),很晕,难道是语言的发展。
[/Quote]
为了你知识保持更新,并且不要毒害你的大脑,请扔掉谭学霸的书。
举国国内之类除了钱能的那本c++还算马虎之外,不要看国内的c++教程,纯粹是为了应付考试加毒害你的脑子,后果不堪设想。
jackzhhuang 2008-12-13
  • 打赏
  • 举报
回复
判断文件是否有效:
if (!file)
{
//无效
}

//有效

是否打的判断开上面有人说了。
lizhaohu 2008-12-13
  • 打赏
  • 举报
回复
infile.open("test.dat",ios::in);
if(!infile)
cout < <"open error" < <endl;
long2041 2008-12-13
  • 打赏
  • 举报
回复
....
sylvanaszm 2008-12-12
  • 打赏
  • 举报
回复
mark
taodm 2008-12-09
  • 打赏
  • 举报
回复
兄弟,扔了你那个破书吧,换本好点的,比如《C++ Primer》第四版。别糟蹋生命了。
cba_v 2008-12-09
  • 打赏
  • 举报
回复
is_open() ?
RobotPassport 2008-12-09
  • 打赏
  • 举报
回复
void open( const char* szName, int nMode = ios::in, int nProt = filebuf::openprot );

Parameters

szName

The name of the file to be opened during construction.

nMode

An integer containing bits defined as ios enumerators that can be combined with the OR ( | ) operator. See the ifstream constructor for a list of the enumerators. The ios::in mode is implied.

nProt

The file protection specification; defaults to the static integer filebuf::openprot. See the ifstream constructor for a list of the other allowed values.

Remarks

Opens a disk file and attaches it to the stream’s filebuf object. If the filebuf object is already attached to an open file, or if a filebuf call fails, the ios::failbit is set. If the file is not found, then the ios::failbit is set only if the ios::nocreate mode was used.

sysabod 2008-12-08
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 sysabod 的回复:]
大家好,可是我的书是说这样检验的(谭浩强的C++程序设计),很晕,难道是语言的发展。
[/Quote]
但是编译就不行啊
nullah 2008-12-08
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 sysabod 的回复:]
大家好,可是我的书是说这样检验的(谭浩强的C++程序设计),很晕,难道是语言的发展。
[/Quote]
你那种方法也可以的
两种都可以
sysabod 2008-12-08
  • 打赏
  • 举报
回复
大家好,可是我的书是说这样检验的(谭浩强的C++程序设计),很晕,难道是语言的发展。
奔跑哥 2008-12-08
  • 打赏
  • 举报
回复
最正确的应该是这样,
infile.open("test.dat",ios::in);
if(!infile)
cout < <"open error" < <endl;
wwwypy 2008-12-08
  • 打赏
  • 举报
回复
顶!!
tangshuiling 2008-12-08
  • 打赏
  • 举报
回复
open函数的返回值是void类型如何可以与0进行比较
正确的写法:
ifstream infile;
infile.open("test.dat",ios::in);
if(infile==NULL) cout <<"open error" <<endl;
wuyu637 2008-12-08
  • 打赏
  • 举报
回复
ifstream 的open函数返回值是void,不能和0做相等判断。
wuyu637 2008-12-08
  • 打赏
  • 举报
回复
void main()
{
ifstream infile;
infile.open("test.dat",ios::in);
if(!infile.is_open()) cout <<"open error" <<endl;
}
ayw215 2008-12-08
  • 打赏
  • 举报
回复

if((infile.open("test.dat",ios::in))==0) cout < <"open error" < <endl;
yshuise 2008-12-08
  • 打赏
  • 举报
回复
觉得不是这儿出错。你贴完代码看看。

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧