C++ 编程,貌似还简单的

来源:百度知道 编辑:UC知道 时间:2024/05/30 14:46:49
Write a program to copy a given text file into another text file in
which the lines are numbered 1,2,3,... with the line number at the
beginning of each line.

写一个程序,将已给的文本从一个文本文件拷贝到另一个文本文件,
并且在每行的开头 标注1,2,3,...的行数。

各位高手帮帮忙吧~~嘻嘻,先谢谢了

/*你在程序的同一个目录下建一个new1.txt的文件,在里面输入几行数据后运行程序试下。*/
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char ch;
int i=1;
ifstream infile("new1.txt",ios::in);
if(! infile)
{
cerr<<"open new1.txt error"<<endl;
exit(1);
}
ofstream outfile("new2.txt",ios::out);
if(! infile)
{
cerr<<"open new2.txt error"<<endl;
exit(1);
}
outfile<<i<<".";
while(infile.get(ch))
{
if(ch!='\n')
{
outfile.put(ch);
cout<<ch;
}
else
{
outfile.put(ch);
cout<<ch;
i++;
outfile<<i<<".";
}
}
cout<<endl;
infile.close;
outfile.close;
return 0;
}