//**********************************************************************************************************
//*程序作者:冰糖
//*完成日期:2019年3月22日
//*章 节:第二章
//*题 号:4
//*题 目:假设你已经做完了本章习题2,编写一个程序,要求用户输入文件名
//* 在屏幕上显示文件的内容。在显示时,每行前面都要带上一个行号和一个冒号。
//* 行号是从一开始,例如:
//* 1:This is a test
//* 2:for you.
//* 3:2011-5-31
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char fileName[20];
char buf[200];
int lineNum=1,count=0;
fstream datafile;
cout<<"请输入文件名:"<<endl;
cin>>fileName;
datafile.open(fileName,ios::in);
//datafile.open("number.txt",ios::in);
if(!datafile)
{
cout<<"打开文件失败!"<<endl;
exit(0);
}
while(datafile.eof()==false)
{
datafile.getline(buf,81);
cout<<lineNum<<":"<<buf<<endl;
count++;
lineNum++;
if(count%24==0)
system("pause");
}
datafile.close();
return 0;
}
number.txt:
11111
22222
333number 33
44444
55555
66666
77777
88888
99999
101010
111111
121212
131313
1414
1515
1616
1717
1818
1919
2020
21
22
23
24
25
26
27
28
29
30