11-9

发布时间 2023-03-28 20:34:55作者: nlkdfgnvfdkl

编写程序实现如下功能:打开指定的一个文本文件,在每一行前加行号后将其输出到另一个文本文件中。

 1 #include <iostream>
 2 #include <string>
 3 #include<string.h>
 4 #include <fstream>
 5 using namespace std;
 6 
 7 int main(){
 8     char path[50];
 9     cout<<"输入文件路径"<<endl;
10     gets(path);
11     ifstream file1(path);
12     ofstream file2("test.txt");
13     string line;
14     int linenumber = 1;
15     while(getline(file1, line)){
16         file2<<linenumber<<":"<<line<<endl;
17         linenumber++;
18     }
19     file1.close();
20     file2.close();
21     
22     char ch;
23     ifstream file3("test.txt");
24     while(file3.get(ch)) cout<<ch;
25     file3.close();
26     return 0;
27 }