4编写一程序,将C++源程序每行前加上行号与一个空格。
#include<iostream>
#include"fstream"
using namespace std;
int main()
{
ofstream file_1("F://learning//c12.cpp"); //打开一个文件,如果没有就自己创建
ifstream file_2("F://learning//c10.cpp"); //读取cpp文件
char ch[1000]; //记录每一行的数据
int row = 0; //记录行数
char tmp;
if (file_1.fail() || file_2.fail()) //如果文件读取失败
{
cout << "文件读取失败" << endl;
}
while (!file_2.eof())
{
file_2.getline(ch,1000);
file_1 << row << " " << ch << endl;
row++;
}
file_2.close();
file_1.close();
system("pause");
return 0;
}