#include <stdio.h>
#include <string.h>
int main()
{
FILE *pInFile;
pInFile = fopen("fileName.txt", "r");
char line[200];
while (fgets(line, sizeof(line), pInFile)) {
printf("\n%s", line);
if (strcmp(line, "C")==1)
printf("Success");
}
return 0;
}
这个程序的目标是在每次读到"C"这一行时打印出“成功”。例如,我的文本文件如下所示:
C
C
C
C
我希望它能够打印出来
C
Success
C
Success
C
Success
C
Success
然而,由于某种原因,它只打印了这个。
C
Success
C
Success
C
Success
C
而且最后一个“Success”被省略了。我完全不知道为什么会这样。
if (strcmp(line, "C")==1)
。strcmp()
不一定会返回 +1。如果第一个参数比第二个参数大,则它可能返回任何大于 0 的数字。将其更改为if (strcmp(line, "C") > 0)
。哦,还要注意空格。 - user529758