问题及代码:
/*
*Copyright (c) 2016,烟台大学计算机学院
*All rights reserved.
*文件名称:main.cpp
*作 者:李磊涛
*完成时间:2016年6月23日
*版 本 号:v1.0
*
*问题描述:将文本文件abc.txt中的所有行加上行号后写到newabc.txt文件中。
*输入描述:无。
*程序输出:写到文件中。
*/
#include<iostream>
#include<cstdlib>
#include<fstream>//
using namespace std;
int main()
{
fstream outfile,infile;
infile.open("abc.txt",ios::in);//
if(!infile)
{
cout<<"abc.txt can't open."<<endl;
abort();
}
outfile.open("newabc.txt",ios::out);//
if(!outfile)
{
cout<<"abc.txt can't open."<<endl;
abort();
}
char buf[80];
int i=1;
while(!infile.eof())//
{
infile.getline(buf,80);
outfile<<i++<<":"<<buf<<endl;//
}
outfile.close();//
infile.close();//
return 0;
}
运行结果:
通过该程序,强化了我对简单程序结构的认识。
学习心得:
期间有很多小错误,要继续写程序争取早日掌握C++。