文件内容统计是C++入门阶段非常实用的练习项目,核心是通过文件流读取目标文件的内容,再结合循环和条件判断完成不同维度的数据统计,不需要依赖额外的第三方库,仅用标准库就能实现。

实现前的准备知识
首先需要了解C++中用于文件操作的标准库fstream,它包含三个核心类:ifstream用于读取文件,ofstream用于写入文件,fstream可同时支持读写。统计文件内容时我们主要使用ifstream类。
还需要掌握几个基础的文件读取方法:
open():打开指定路径的文件,参数是文件路径字符串is_open():判断文件是否成功打开,返回布尔值get():读取单个字符getline():读取一行内容,直到遇到换行符close():关闭打开的文件,释放资源
基础统计功能实现
统计文件总行数
统计行数的逻辑是逐行读取文件内容,每读取一行就计数加1,直到文件读取结束。下面是完整的实现代码:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int count_file_lines(const string& file_path) {
ifstream file;
// 打开目标文件
file.open(file_path);
// 判断文件是否成功打开
if (!file.is_open()) {
cout << "无法打开文件: " << file_path << endl;
return -1;
}
string line;
int line_count = 0;
// 逐行读取,每读一行计数加1
while (getline(file, line)) {
line_count++;
}
// 关闭文件
file.close();
return line_count;
}
int main() {
string file_path = "test.txt";
int lines = count_file_lines(file_path);
if (lines != -1) {
cout << "文件总行数: " << lines << endl;
}
return 0;
}
统计文件总字符数
统计字符数可以选择逐字符读取,也可以选择逐行读取后累加每行的长度,两种方式都可以,逐字符读取的统计结果更准确,会包含换行符等不可见字符。下面是逐字符读取的实现代码:
#include <iostream>
#include <fstream>
using namespace std;
int count_file_chars(const string& file_path) {
ifstream file;
file.open(file_path);
if (!file.is_open()) {
cout << "无法打开文件: " << file_path << endl;
return -1;
}
char c;
int char_count = 0;
// 逐字符读取,每读一个字符计数加1
while (file.get(c)) {
char_count++;
}
file.close();
return char_count;
}
int main() {
string file_path = "test.txt";
int chars = count_file_chars(file_path);
if (chars != -1) {
cout << "文件总字符数: " << chars << endl;
}
return 0;
}
进阶统计功能实现
统计文件单词数量
单词统计的逻辑是判断字符是否为字母或数字,连续的有效字符组成一个单词,遇到非有效字符时如果之前有正在记录的单词,就计数加1。下面是实现代码:
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
int count_file_words(const string& file_path) {
ifstream file;
file.open(file_path);
if (!file.is_open()) {
cout << "无法打开文件: " << file_path << endl;
return -1;
}
char c;
int word_count = 0;
bool in_word = false; // 标记是否正在读取一个单词
while (file.get(c)) {
// 判断当前字符是否为字母或数字
if (isalnum(c)) {
if (!in_word) {
// 进入新单词,计数加1
word_count++;
in_word = true;
}
} else {
// 遇到非字母数字,标记单词结束
in_word = false;
}
}
file.close();
return word_count;
}
int main() {
string file_path = "test.txt";
int words = count_file_words(file_path);
if (words != -1) {
cout << "文件单词数量: " << words << endl;
}
return 0;
}
整合所有统计功能
我们可以把上面的几个统计函数整合到一个程序中,一次性输出文件的总行数、总字符数、单词数量,方便直接使用。整合后的代码如下:
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;
// 统计行数
int count_lines(const string& file_path) {
ifstream file(file_path);
if (!file.is_open()) return -1;
string line;
int count = 0;
while (getline(file, line)) count++;
file.close();
return count;
}
// 统计字符数
int count_chars(const string& file_path) {
ifstream file(file_path);
if (!file.is_open()) return -1;
char c;
int count = 0;
while (file.get(c)) count++;
file.close();
return count;
}
// 统计单词数
int count_words(const string& file_path) {
ifstream file(file_path);
if (!file.is_open()) return -1;
char c;
int count = 0;
bool in_word = false;
while (file.get(c)) {
if (isalnum(c)) {
if (!in_word) {
count++;
in_word = true;
}
} else {
in_word = false;
}
}
file.close();
return count;
}
int main() {
string file_path = "test.txt";
int lines = count_lines(file_path);
int chars = count_chars(file_path);
int words = count_words(file_path);
if (lines != -1 && chars != -1 && words != -1) {
cout << "文件统计结果:" << endl;
cout << "总行数: " << lines << endl;
cout << "总字符数: " << chars << endl;
cout << "单词数量: " << words << endl;
} else {
cout << "文件打开失败,请检查路径是否正确" << endl;
}
return 0;
}
注意事项
在实际使用时需要注意几个问题:
- 文件路径要填写正确,如果是相对路径,要放在程序运行的目录下
- 打开文件后一定要判断
is_open()的返回值,避免文件不存在导致的运行错误 - 文件操作完成后要及时调用
close()关闭文件,释放系统资源 - 统计单词的逻辑可以根据实际需求调整,比如是否把下划线也算作单词的一部分,只需要修改
isalnum的判断条件即可