我正在尝试从文本文档中搜索关键字,我已尝试排除空格、换行符和制表符。我能够从文件中读取输入,但无法更改 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/