//6.8
#include<iostream>
#include<fstream>
using namespace std;
void main68()
{
ifstream inFile;
ifstream fin;
int count=0;
inFile.open("2.txt"); //请将文本文件放到当前路径
if(!inFile.is_open())
{
cout<<"Open Failed!"<<endl;
}
char ch;
inFile>>ch;
while(inFile.good())
{
count++;
inFile>>ch;
}
if(inFile.eof())
{
cout<<"count="<<count<<endl;
}
system("pause");
}

本文介绍了一个使用C++进行文件读取的基本示例,通过打开文本文件并逐字符读取来计数文件中的字符数量。展示了如何使用ifstream类进行文件操作,并处理可能遇到的错误情况。

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



