Yang_Fancy 2015-08-23 02:35 采纳率: 0%
浏览 1595

c++ primer 习题8.6 文件总是打开失败

#include "stdafx.h"
#include
#include
#include
using namespace std;
istream &f(istream &);

int _tmain(int argc, _TCHAR* argv[])
{
string filename="hello";
ifstream input;
input.open(filename.c_str(),ios::binary);
if(input.is_open())
{
cout<<"opened"<<endl;
}
if(!input)
if(input.bad())
cout<<"bad"<<endl;
if(input.fail())
cout<<"fail"<<endl;
return -1;
f(input);
system("pause");
return 0;
}

istream &f(istream &in)
{
string temp;
while(in>>temp,!in.eof())
{
if(in.bad())
throw runtime_error("IO stream corrupted");
if(in.fail())
{
cerr<<"bad data,try again!";
in.clear();
in.setstate(istream::eofbit);
continue;
}
cout<<temp<<endl;
}
in.clear();
return in;

}


代码如上,总是跳到文件打开失败语句,hello.txt文件我放在与exe文件同一个目录下。
百思不得其解啊?为什么呢?

  • 写回答

3条回答 默认 最新

  • Yang_Fancy 2015-08-23 04:22
    关注

    终于在我的不懈努力下,解决这个问题啦!!!!还是文件路径的问题,把它改成绝对路径就可以了,但是还是不明白放到Debug文件夹为什么不可以?

    评论

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3