c++中打开文本文件,读取字符数量是一个比较简单的操作。
#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream fin;
fin.open("dcb.txt");
char ch;
int counter = 0;
while (fin && fin.get(ch)) {
cout << ch;
counter++;
}
fin.close();
cout << "\ntotal litters of this file:" << counter << endl;
}
输出
hello world
who you are?
total litters of this file:24

本文介绍了一个使用C++编程语言实现的简单程序,该程序能够打开一个文本文件并逐字符读取,同时统计文件中的总字符数。通过使用标准库中的`fstream`类,程序展示了如何打开文件、读取内容以及关闭文件。
1236

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



