Enter the code:
/**copyright (c) 2015, College of Computer and Control engineering, Yantai University *all rights reserved.* file name: sum123.cpp* Creator: Lin Haiyun * Completion Date: August 18, 2015 * version number: v2.0* Problem Description: Add line numbers to all lines in the text file Abc.txt and write to the Newabc.txt file. * Program input: File read. * Program output: Add line numbers to all lines in the text file and write to the new file. */#include <iostream> #include <cstdlib> #include <fstream>using namespace Std;int main () { FStream Outfile,infile; Infile.open ("Abc.txt", ios::in); (1) if (!infile) { cout<< "Can ' t open the file." <<endl; Abort (); } Outfile.open ("Newabc.txt", ios::out);//(2) if (!outfile) { cout<< "Can ' t open the file." <<endl; Abort (); } Char buf[800]; int i=1; while (!infile.eof ())//(3) { infile.getline (buf,800);//(4) outfile<<i++<< ":" << buf<<endl; (5) } infile.close (); Outfile.close (); return 0;}
Operation Result:
Abc.tex File Data
Newabc.tex
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Add: Play file 2--add line numbers to all lines in a text file and write to a new file