我当前的任务是编写一个函数,该函数具有一个FILE指针和一个指向字符串的指针.该函数应分析该字符串在给定文件中出现的次数,并以整数形式返回值.它还需要注意区分大小写.在我当前的程序中,我将单词"Dog"作为要在文件中找到的字符串.但是,即使.txt文件中有三次狗这个词,它也一直给我0.这是我在这里的第一篇帖子,我查看了其他关于这个话题的帖子,但他们无法修复它.

这就是我所try 的:

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

int searchAndCount(FILE *fp, const char *searchWord) {
    int count = 0;
    char buffer[4096];
    
    while ((fgets(buffer, sizeof(buffer), fp)) != NULL) { 
        buffer[strlen(buffer) - 1] = '\0';
        if (strcmp(buffer, searchWord) == 0) {
            count++;
        }
    }
    return count;
}

int main() {
    FILE *fp;
    int searchedWord;
    const char *c = "dog";
    
    fp = fopen("test.txt", "r");
    if (fp == NULL) {
        perror("File couldn't open properly");
        return 1;
    }
    searchedWord = searchAndCount(fp, c);
    printf("The word 'dog' occurs %d-times in the file\n", searchedWord);

    fclose(fp);

    return 0;
}

我的test.txt如下所示:

dog dog dogoggo dog.

我明白了这一点:

The word 'dog' occurs 0-times in the file

编辑: 所以从 comments 来看,我似乎需要实现strtok()个我将研究这个函数.

但使用:

int searchAndCount(FILE *fp, const char *searchWord) {
    int count = 0;
    char buffer[4096];
    
    while ((fscanf(fp, "%4095s", buffer)) == 1) { 
        if (strcmp(buffer, searchWord) == 0) {
            count++;
        }
    }
    return count;
}

或多或少解决了问题,而"狗".不会因为点而被计算在内.

解决方案:

int searchAndCount(FILE *fp, const char *searchWord) {
    int count = 0;
    char buffer[4096];
    
    for (;;) {
        fscanf(fp, "%*[^a-zA-Z]");
        if (fscanf(fp, "%4095[a-zA-Z]", buffer) != 1)
            break;
        if (strcmp(buffer, searchWord) == 0)
            count++;
    }
    return count;
}

推荐答案

由于您要匹配的是单词而不是完整的行,因此应该使用fscanf而不是fgets:

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

int searchAndCount(FILE *fp, const char *searchWord) {
    int count = 0;
    char buffer[4096];
    
    // %4095s means store up to 4095 characters into buffer
    // before the null terminator
    while (fscanf(fp, "%4095s", buffer) == 1) { 
        if (strcmp(buffer, searchWord) == 0) {
            count++;
        }
    }
    return count;
}

int main(void) {
    FILE *fp;
    int count;
    const char *str = "dog";
    
    fp = fopen("test.txt", "r");
    if (fp == NULL) {
        perror("File couldn't open properly");
        return 1;
    }
    count = searchAndCount(fp, str);
    printf("The word 'dog' occurs %d times in the file\n", count);

    fclose(fp);

    return 0;
}

如果你想忽略标点符号,这里有一个更详细的版本:

int searchAndCount(FILE *fp, const char *searchWord) {
    int count = 0;
    char buffer[4096];
    
    for (;;) {
        fscanf(fp, "%*[^a-zA-Z]");
        if (fscanf(fp, "%4095[a-zA-Z]", buffer) != 1)
            break;
        if (strcmp(buffer, searchWord) == 0)
            count++;
    }
    return count;
}

C++相关问答推荐

获取二维数组的最大元素

带双指针的2D数组

exit(EXIT_FAILURE):Tcl C API类似功能

C编译器是否遵循restrict的正式定义?

为什么即使在强制转换时,此代码也会溢出?

如何在不使用其他数组或字符串的情况下交换字符串中的两个单词?

非常大的数组的大小

试图从CSV文件中获取双精度值,但在C++中始终为空

是什么让numpy.sum比优化的(自动矢量化的)C循环更快?

Sizeof(&Q;字符串&Q;)的正确输出是什么?

tick.q中的Kdb+键控表语法

强制转换变量以在 struct 中蚕食

如何在C++中安全地进行浮点运算

通过对一个大的Malloc内存进行切片来使用Malloc的内存片

如何在MSVC中使用intSafe.h函数?

如何使crc32的结果与cksum匹配?

";错误:寄存器的使用无效;当使用-masm=intel;在gcc中,但在AT&;T模式

为什么INT_MIN是在c语言的头文件limits.h中定义的(-INT_MAX-1)而不是直接使用-2147483648

为什么程序在打印每个数字之前要等待所有输入?

为什么实现文件中的自由函数默认没有内部链接?