编辑代码

#include <stdio.h>
#include <stdlib.h> // 添加这行以包含exit函数的定义

int main() {
    FILE *fp;
    char ch;
    int chars, nums, other;
    chars = nums = other = 0;

    // 打开文件以读取内容
    if ((fp = fopen("test.txt", "r")) == NULL) {
        printf("file open error\n");
        exit(1); // 使用非零值表示程序出错
    }

    while ((ch = fgetc(fp)) != EOF) {
        if (ch >= '0' && ch <= '9')
            nums++;
        else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
            chars++;
        else
            other++;
    }

    // 关闭文件
    if (fclose(fp)) {
        printf("file close error\n");
    }

    printf("字母:%d个,数字:%d个,其他:%d个。\n", chars, nums, other);
    return 0; // 返回0表示程序成功运行
}