![]() |
#2
ciweitou1632010-05-27 19:16
|

#include "stdafx.h"
#include "fstream"
#include "iostream"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char ch,a[100];
ifstream file1;
ofstream file2;
file1.open("file1.txt",ios::in);
if(file1)
{
int i=0;
while((ch=file1.eof())==0)
{
file1.getline(a,100);
cout<<a<<endl;
file2.open("file2.txt",ios::out|ios::app);
file2<<i+1<<":"<<a<<endl;
file2.close();
i++;
}
}
else
cout<<"文件打开失败"<<endl;
file1.close();
return 0;
}
以上代码目的将file1文件内容逐行读取出来读出一行后打开文件file2将读出内容写入file2中。
程序编译通过但是读出内容没有写到file2中,这是为什么?请高手帮忙回答下,谢谢了
[ 本帖最后由 tonlin 于 2010-5-27 13:27 编辑 ]