编程菜鸟的日记-初学尝试编程-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.\n";
		exit(EXIT_FAILURE);
	}
	double count=0;
	inFile>>ch;
	while(inFile.good())
	{
		++count;
		inFile>>ch;
	}
	if(inFile.eof())
		cout<<"End of file reached.\n";
	else if(inFile.fail())
		cout<<"Input terminated by char mismatch.\n";
	else 
		cout<<"Input terminated for unknown reason.\n";
	if (count==0)
		cout<<"No char processed.\n";
	else
		cout<<"Items read: "<<count<<endl;
	inFile.close();
	system("pause");
	return 0;
}

 

posted @ 2013-12-03 01:33  编程的爬行者  阅读(134)  评论(0编辑  收藏  举报