//输入一个文件名,逐个字符的读取文件,然后显示所有的字符总数
#include<iostream>
#include<fstream> //文件输出,也要用名称空间std;
#include<cstdlib> //支持exit()
int main()
{
using namespace std;
char filename[60];
ifstream inFile; //声明ifstream变量
cout <<"Please enter filename: ";
cin.getline(filename,60);
inFile.open(filename);//将ifstream对象和文件链接起来
if(!inFile.is_open()) //检查文件是否成功打开
{
cout <<"Could not open the file "<<filename<<endl;
cout <<"program terminating.\n";
//cin.get();
exit(EXIT_FAILURE); //终止程序
}
int number;
char charr1;
number = 0;
inFile.get(charr1); //ifstream对象与cin的输入方法一样
while(inFile.good()) //检查可能存在的问题,没有is_open广泛
{
number++;
inFile.get(charr1);
}
if(inFile.eof()) //只能检查时候遇到EOF,遇到返回ture
{
cout <<"End of file reached.\n";
}
else if(inFile.fail())//检查类型不匹配和EOF,遇到返回false
{
cout <<"Input termi
【C++】文件输入iftream
最新推荐文章于 2020-04-17 14:52:47 发布