C++问题~2

来源:百度知道 编辑:UC知道 时间:2024/05/05 08:37:19
2. 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.

// 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.

#include<iostream>
#include<string>
#include<fstream>
using namespace std;

void main()
{
ifstream infile("test.txt");
ofstream oufile("test2.txt");

int i=0;
string curLine;
while(getline(infile,curLine))
{
oufile<<++i<<":"<<curLine<<endl;
}
}

/*test result
text.txt:
hello
world

text2.txt:
1:hello
2:world

*/