编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习8

#include <iostream>
#include <fstream>
#include <cstdlib>
const int SIZE=20;
using namespace std;
int main()
{
	char filename[SIZE];
	char ch;
	ifstream inFile;//inFile 是有一个ifstream对象
	cout<<"Enter name of file: ";
	cin.get(filename,SIZE);
	inFile.open(filename);//inFile用来读取Lynn.txt文件
	if(!inFile.is_open())
	{
		cout<<"Could not open the file "<<filename<<endl;
		cout<<"Program terminating.
";
		exit(EXIT_FAILURE);
	}
	double count=0;
	inFile>>ch;
	while(inFile.good())
	{
		++count;
		inFile>>ch;
	}
	if(inFile.eof())
		cout<<"End of file reached.
";
	else if(inFile.fail())
		cout<<"Input terminated by char mismatch.
";
	else 
		cout<<"Input terminated for unknown reason.
";
	if (count==0)
		cout<<"No char processed.
";
	else
		cout<<"Items read: "<<count<<endl;
	inFile.close();
	system("pause");
	return 0;
}
原文地址:https://www.cnblogs.com/lynnycy/p/3455034.html