首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

如何老是打开失败啊请详细解释吧

2012-04-15 
怎么老是打开失败啊!请详细解释吧!高手进#includeiostream#includefstreamusing namespace stdint ma

怎么老是打开失败啊!请详细解释吧!高手进
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char ch;
ifstream fin("file1.txt");
if(!fin){cout<<"cannot open file";return 1;}
cout<<"file1.txt:"<<endl;
while(fin.get(ch))
cout.put(ch);
cout<<endl;
fin.close ();
fin.open("file1.txt");
ofstream fout("file2.txt");
if(!fout){cout<<"cannot open file2!";return 2;}
while(fin.get(ch))
fout.put(ch);
fin.close();
fout.close();
fin.open("file2.txt");
while(fin.get(ch))
cout.put(ch);
cout<<endl;
fout.close ();
return 0;
}



[解决办法]
很明显 是文件不存在,或路径错误

ofstream是打开文件用于写,若文件不存在 则自动创建文件
ifstream是打开文件用于读,若文件不存在 则返回失败

热点排行