自考新教材-p310

源程序:

#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
int main()
{
char ch, filename[20];
int count = 0;
bool newline = true;
cout << "请输入文件名:";
cin >> filename;
ifstream inFile(filename, ios::in);
if (!inFile)
{
cout << "打开文件失败" << endl;
return 0;
}
while ((ch = inFile.get()) != EOF)
{
if (newline)
{
cout << setw(4) << ++count << ':';
newline = false;
}
if (ch == '\n')
newline = true;
cout << ch;
}
inFile.close();
system("pause");
return 0;
}

运行结果:

 

posted @ 2020-02-05 13:27  bobo哥  阅读(132)  评论(0编辑  收藏  举报