*文件名称:test.cpp
*作者:于凯
*完成日期:2015年6月10日
*版本号:v1.0
*/
#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的文件,并逐字符读取内容,统计文件中字符总数。

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



