读书人

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

发布时间: 2012-04-15 18:39:21 作者: rapoo

怎么老是打开失败啊!请详细解释吧!高手进
#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是打开文件用于读,若文件不存在 则返回失败

读书人网 >C++

热点推荐