C++文件读入失败的原因

ts118 2009-02-22 09:46:45

#include<iostream>
#include<string>
#include<vector>
#include<fstream>

using namespace std;

int get(string infile,vector<string>& svec)
{
ifstream in(infile.c_str());

if(!in)
return 1;

string str;

while(getline(in,str))
svec.push_back(str);

in.close();

if(in.bad())
return 2;
if(in.fail())
return 3;
if(in.eof())
return 4;
}


代码如上,经测试读入数据失败时,文件中的所有数据都已经保存在vector<string>中了。
想知道导致数据读入失败的原因是什么?或者是我写的有问题?
用的是VS2008

30分,分数不多!
...全文
346 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
bfhtian 2009-02-22
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 ts118 的回复:]
请教2楼,3楼。
while(get(in,str))
svec.push_back(str)
遇到badbit,failbit是不是也是循环结束的条件?

另外close()关闭了文件,那in依然起作用吗?

这段代码 是C++ Primer上 一道习题的答案,请各位帮忙解答一下!
[/Quote]
是循环结束的条件
ts118 2009-02-22
  • 打赏
  • 举报
回复
请教2楼,3楼。
while(get(in,str))
svec.push_back(str)
遇到badbit,failbit是不是也是循环结束的条件?

另外close()关闭了文件,那in依然起作用吗?

这段代码 是C++ Primer上 一道习题的答案,请各位帮忙解答一下!
  • 打赏
  • 举报
回复
你试下在while里加点记录的打印语句,看看是一开始输出流的状态就是失败,还是读到最后失败啊.
还有就是最好在使用getlien前,in.clear()一下.
帅得不敢出门 2009-02-22
  • 打赏
  • 举报
回复
貌似 没有必要如此测试
waizqfor 2009-02-22
  • 打赏
  • 举报
回复
[Quote=引用楼主 ts118 的帖子:]
C/C++ code
#include<iostream>
#include<string>
#include<vector>
#include<fstream>

using namespace std;

int get(string infile,vector<string>& svec)
{
ifstream in(infile.c_str());

if(!in)
return 1;

string str;

while(getline(in,str))
svec.push_back(str);

in.close();

if(in.bad())
return 2;
if(in.fail())
retur…
[/Quote]
LZ你文件都关闭了 还用
if(in.bad())
return 2;
if(in.fail())
retur…
检测有什么用呢 把他们放到文件打开的时候检测啊
a_rockboy 2009-02-22
  • 打赏
  • 举报
回复
in.close();

if(in.bad())
return 2;
if(in.fail())
return 3;
if(in.eof())
return 4;

close之后再测试有什么用啊。不用后面的3个。或者挪到适当的位置。其中
if(in.eof())
return 4;
是要放在getline之前的。
ouyh12345 2009-02-22
  • 打赏
  • 举报
回复
get函数是怎样调用的
在while循环后,把svec里的内容打印出来
arong1234 2009-02-22
  • 打赏
  • 举报
回复
到eof也就是in.bad为true是你循环结束的条件啊?楼主那些测试到底想干吗?这个错误是必然的啊
hityct1 2009-02-22
  • 打赏
  • 举报
回复
有问题自己先看msdn,再看源代码。

关于getline有一句:
If the function extracts no elements, it calls setstate(failbit).


测试,返回3。没有问题。


内部调用了此函数:

template<class _Elem,
class _Traits,
class _Alloc> inline
basic_istream<_Elem, _Traits>& __CLRCALL_OR_CDECL getline(
basic_istream<_Elem, _Traits>& _Istr,
basic_string<_Elem, _Traits, _Alloc>& _Str,
const _Elem _Delim)
{ // get characters into string, discard delimiter
typedef basic_istream<_Elem, _Traits> _Myis;
ios_base::iostate _State = ios_base::goodbit;
bool _Changed = false;
const typename _Myis::sentry _Ok(_Istr, true);

if (_Ok)
{ // state okay, extract characters
_TRY_IO_BEGIN
_Str.erase();
const typename _Traits::int_type _Metadelim =
_Traits::to_int_type(_Delim);
typename _Traits::int_type _Meta = _Istr.rdbuf()->sgetc();

for (; ; _Meta = _Istr.rdbuf()->snextc())
if (_Traits::eq_int_type(_Traits::eof(), _Meta))
{ // end of file, quit
_State |= ios_base::eofbit;
break;
}
else if (_Traits::eq_int_type(_Meta, _Metadelim))
{ // got a delimiter, discard it and quit
_Changed = true;
_Istr.rdbuf()->sbumpc();
break;
}
else if (_Str.max_size() <= _Str.size())
{ // string too large, quit
_State |= ios_base::failbit;
break;
}
else
{ // got a character, add it to string
_Str += _Traits::to_char_type(_Meta);
_Changed = true;
}
_CATCH_IO_(_Istr)
}

if (!_Changed)
_State |= ios_base::failbit;
_Istr.setstate(_State);
return (_Istr);
}
ts118 2009-02-22
  • 打赏
  • 举报
回复
可能我没表达清楚吧,这段代码是C++Primer上一道习题的答案。
问题是在while()结束后,in有两种状态,分别是falibit和eofbit,而且文件中的所有内容都已经被读入vector<string>了。
大家说是close()的问题,我觉得有问题,close()不能清楚所有的状态,不然为什么很多代码close()之后还要clear()呢?

今天下午借来了侯先生的《C++标准程序库》,在P598找到了这样一段话:
eofbit和failbit常常同时出现,因为在end-of-file之后再试图读取数据,就会检测出end-of-file状态。读取最后一个字符时,eofbit并未设立,但再一次试图读取字符时,就会导致eofbit和failbit同时成立,因为读取操作也失败了。

我觉得问题就是出在这里。

多谢各位帮忙解答,欢迎继续讨论。

明天上午结帖给分。

64,649

社区成员

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

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