C语言中,统计文本文件中的字符、单词和行数是一个常见的任务。可以使用fgetc()逐字符读取,可以使用fgets()逐行读取,也使用fread()块读取,还可以通过使用fscanf()格式化读取。

1、使用fgetc()逐字符读取

使用fgetc()逐个读取字符,通过检查空格、换行符和制表符来判断单词和行。

#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "r");
    if (file == NULL) {
        perror("Error opening file");
        return 1;
    }

    int chars = 0, words = 0, lines = 0;
    char ch;
    int in_word = 0;

    while ((ch = fgetc(file)) != EOF) {
        chars++;
        if (ch == '\n') {
            lines++;
        }
        if (ch == ' ' || ch == '\n' || ch == '\t') {
            in_word = 0;
        } else if (in_word == 0) {
            in_word = 1;
            words++;
        }
    }

    fclose(file);
    printf("Characters: %d\nWords: %d\nLines: %d\n", chars, words, lines);
    return 0;
}

2、使用fgets()逐行读取

使用fgets()逐行读取文件内容,统计字符数时考虑了每行的长度。

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main() {
    FILE *file = fopen("example.txt", "r");
    if (file == NULL) {
        perror("Error opening file");
        return 1;
    }

    int chars = 0, words = 0, lines = 0;
    char buffer[1024];
    
    while (fgets(buffer, sizeof(buffer), file) != NULL) {
        lines++;
        chars += strlen(buffer);
        int in_word = 0;
        for (int i = 0; buffer[i] != '\0'; i++) {
            if (isspace(buffer[i])) {
                in_word = 0;
            } else if (in_word == 0) {
                in_word = 1;
                words++;
            }
        }
    }

    fclose(file);
    printf("Characters: %d\nWords: %d\nLines: %d\n", chars, words, lines);
    return 0;
}

3、使用fread()块读取

使用fread()一次性读取较大的数据块来提高效率,并在读取的块中统计字符、单词和行数。

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main() {
    FILE *file = fopen("example.txt", "r");
    if (file == NULL) {
        perror("Error opening file");
        return 1;
    }

    int chars = 0, words = 0, lines = 0;
    char buffer[1024];
    size_t bytesRead;
    int in_word = 0;

    while ((bytesRead = fread(buffer, 1, sizeof(buffer), file)) > 0) {
        for (size_t i = 0; i < bytesRead; i++) {
            chars++;
            if (buffer[i] == '\n') {
                lines++;
            }
            if (isspace(buffer[i])) {
                in_word = 0;
            } else if (in_word == 0) {
                in_word = 1;
                words++;
            }
        }
    }

    fclose(file);
    printf("Characters: %d\nWords: %d\nLines: %d\n", chars, words, lines);
    return 0;
}

4、使用fscanf()格式化读取

使用fscanf()逐个单词读取文件内容,计算每个单词的长度来统计字符数。

#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "r");
    if (file == NULL) {
        perror("Error opening file");
        return 1;
    }

    int chars = 0, words = 0, lines = 0;
    char word[1024];

    while (fscanf(file, "%1023s", word) == 1) {
        words++;
        chars += strlen(word);
        int i = 0;
        while (word[i] != '\0') {
            if (word[i] == '\n') {
                lines++;
            }
            i++;
        }
    }

    fclose(file);
    printf("Characters: %d\nWords: %d\nLines: %d\n", chars, words, lines);
    return 0;
}

5、使用低级别 I/O 函数read()

使用 read() 函数进行低级别文件读取操作,适合需要直接控制文件 I/O 的情况。

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>

int main() {
    int file = open("example.txt", O_RDONLY);
    if (file == -1) {
        perror("Error opening file");
        return 1;
    }

    int chars = 0, words = 0, lines = 0;
    char buffer[1024];
    ssize_t bytesRead;
    int in_word = 0;

    while ((bytesRead = read(file, buffer, sizeof(buffer))) > 0) {
        for (ssize_t i = 0; i < bytesRead; i++) {
            chars++;
            if (buffer[i] == '\n') {
                lines++;
            }
            if (isspace(buffer[i])) {
                in_word = 0;
            } else if (in_word == 0) {
                in_word = 1;
                words++;
            }
        }
    }

    close(file);
    printf("Characters: %d\nWords: %d\nLines: %d\n", chars, words, lines);
    return 0;
}

推荐文档

相关文档

大家感兴趣的内容

随机列表