源程序:
#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 == '
')
newline = true;
cout << ch;
}
inFile.close();
system("pause");
return 0;
}
运行结果: