简体   繁体   English

在C中使用Strcmp

[英]Using Strcmp in C

I am trying to search for key words from in a text document I have tried to exclude white space, new lines, and tabs. 我正在尝试从文本文档中搜索关键词,而我试图排除空格,换行和制表符。 I am able to read inputs from the file but I can not get my strcmp and the counter to change. 我能够从文件中读取输入,但无法获取我的strcmp和计数器。 I tried to use printf statements to locate the issue but I am still lost. 我尝试使用printf语句查找问题,但仍然迷路。 If its a syntax issue let me know. 如果是语法问题,请告诉我。 Any help would be appreciated. 任何帮助,将不胜感激。

Heres the block of code 继承人的代码块

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);

It think your problem is right here: 它认为您的问题就在这里:

if(p = 0 ) 

It should be: 它应该是:

if(p == 0 ) 

Turn up your compiler warning level enough (like -Wall in gcc) and you may get a warning for unintended assignments. 充分提高编译器警告级别(例如gcc中的-Wall ),可能会收到有关意外分配的警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM