问题:
解答:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string filename;
ifstream stream;
char read_char;
int count=0;
cout << "请输入要打开的文件:";
getline(cin, filename);
stream.open(filename);
if (!stream.is_open())
{
cout << "打开文件失败!";
exit(EXIT_FAILURE);
}
while (!stream.eof())
{
stream >> read_char;
count++;
}
cout << filename << "文件含有" << count << "个字符" << endl;
stream.close();
return 0;
}
运行结果:
考查点:
- 读文件操作
注意:
- 文件结束符
- 根据数据类型读相应的字节