读书人

个文件流对象为啥不能关联2个文件

发布时间: 2014-01-01 18:12:08 作者: rapoo

个文件流对象,为什么不能关联2个文件
#include<iostream>
#include<fstream>
using namespace std;

int main(){
char temp[200];
ifstream fp;

fp.open("d:\\root.txt");
if(!fp){
cout<<" error"<<endl;
exit(1);
}
while(fp)
{
fp.getline(temp,200);
cout<<temp<<endl;
}
fp.close();

fp.open("d:\\out.txt");
if(!fp){
cout<<" error"<<endl;
exit(1);
}
while(fp)
{
fp.getline(temp,200);
cout<<temp<<endl;
}
fp.close();
}
为什么fp.open("d:\\out.txt");时,文件无法打开,即fp为假
[解决办法]
请记得clear再open第二个。
另外,复用流对象,可以说永远是一个糟糕想法。

读书人网 >C++

热点推荐