您的位置:首页 > 编程语言 > C语言/C++

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

2013-12-03 01:33 417 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐