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 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)