c - 在 C 中使用 Strcmp

标签 c string

我正在尝试从文本文档中搜索关键字,我已尝试排除空格、换行符和制表符。我能够从文件中读取输入,但无法更改 strcmp 和计数器。我尝试使用 printf 语句来定位问题,但我仍然迷路了。如果它是语法问题,请告诉我。任何帮助,将不胜感激。

这是代码块

FILE *file2 = fopen( argv[2], "r");

if (file2 == NULL) {
    printf("\nFile 2 could not open\n");
} else {
    do {
        // treat all new line as spaces.   
        x = fgetc( file2 );

        if (x == '\n') {
            x = 32;
        }

        if (x == '\t') {
            x = 32;
        }

        if(x != 32 ) {
            temp[l] = x;

        } else { 
            temp[l] == '\0';

            printf("\t%s", temp);

            for(a=0; a < countw; a++) {
                p = strcmp(swords[a].word,temp);
                if (p = 0) {
                    swords[a].count++;
                }
                printf("\nstop%i\n",a);
            }

            for(b=0; b<l; b++) {
                temp[b] = '\0';
            }
            l = 0; 
        }
    } while (x != EOF);
}

fclose(file2);

最佳答案

它认为你的问题就在这里:

if(p = 0 ) 

应该是:

if(p == 0 ) 

将您的编译器警告级别调高(例如 gcc 中的 -Wall),您可能会收到有关意外分配的警告。

关于c - 在 C 中使用 Strcmp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23160491/

相关文章:

c - 变量参数函数的堆栈激活框架如何工作?

ios - 将 Swift 字符串转换为数组

c - Ubuntu 信号量 : Segmentation fault (core dumped)

c - 如何在 C 中使用 java webservice

c++ - 如何用空格替换 std::string 中的所有非字母字符(数字和特殊字符)

c++ - 如何使用 Levenshtein 距离字符串度量

python - 如何在字符串中每2个字符后插入一个字符

python - 如何用零填充字符串?

c - 如何为 BN_generate_prime 播种 PRNG

c - 在 C 中使用带有结构的指定初始值设定项