8.编写一个程序,它打开一个文件文件,逐个字符地读取该文件,直到到达文件末尾,然后指出该文件中包含多少个字符。
#pragma region 第五章练习8
#if 1
#include<iostream>
#include<fstream>
#include <cstdlib>
int main() {
using namespace std;
const int SIZE = 50;
char filename[SIZE];
ifstream inFile;
cout << "请输入文件名字";
cin.getline(filename, SIZE);
inFile.open(filename);
if (!inFile.is_open())
{
cout << "无法打开文件" << filename << endl;
cout << "程序终止.";
exit(EXIT_FAILURE);
}
char ch;
int cntch = 0;
inFile >> ch;
while (inFile.good())
{
++cntch;
inFile >> ch;
}
if (inFile.eof())
{
cout << "读到文件末尾了。\n";
}
else if (inFile.fail())
{
cout << "读取类型错误不匹配而终止。\n";
}
else
{
cout << "输出错误,原因不明.\n";
}
if (cntch == 0)
{
cout << "未处理数据\n";
}
else
{
cout << "读取到的字符数量是:" << cntch <<" 个.\n" << endl;
}
return 0;
}
#endif
#pragma endregion