问题及代码
#include <iostream>
#include <cstdlib>
#include <fstream>// (1)
using namespace std;
int main()
{
fstream file;
file.open("abc.txt", ios::in); // (2)
if(!file) {
cout<<"abc.txt can’t open."<<endl;
exit(1);
}
char ch;
int i=0;
while( !file.eof()) // (3)
{
file.get(ch);
++i; // (4)
}
cout<<"Character: "<<i<<endl;
file.close();// (5)
return 0;
}
本文介绍了一个使用C++进行文件读取的基本示例,通过fstream类打开一个名为abc.txt的文本文件,并统计文件中的字符总数。
647

被折叠的 条评论
为什么被折叠?



