什么';这个C代码怎么了?

什么';这个C代码怎么了?,c,strstr,C,Strstr,我已经编写了一个C程序来对一些字符串进行分类。我使用文件流读写文件。但我发现了一个问题。通常它应该对字符串进行分类,但实际上不是。我认为这个代码是好的,所以我找不到问题。请帮帮我 #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char line[80]; //create a string FILE *r_in = fopen("spooky.c

我已经编写了一个C程序来对一些字符串进行分类。我使用文件流读写文件。但我发现了一个问题。通常它应该对字符串进行分类,但实际上不是。我认为这个代码是好的,所以我找不到问题。请帮帮我

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

int main()
{
    char line[80]; //create a string
    FILE *r_in = fopen("spooky.csv", "r"); // this is the source file
    FILE *w_ufo = fopen("ufo.txt", "w"); // strings with "UFO" will be written to here
    FILE *w_disapp = fopen("disappearance.txt", "w"); // strings with "Disappearance" will be written to here  
    FILE *w_others = fopen("others.txt", "w"); // others will be written to here

    while (fscanf(r_in, "%79[\n]\n", line) == 1)
    {
        if(strstr(line, "UFO")) // I think here is the problem (with strstr())
            fprintf(w_ufo, "%s\n", line);
        else if(strstr(line, "Disappearance"))
            fprintf(w_disapp, "%s\n", line);
        else
            fprintf(w_others, "%s\n", line);
    }

    fclose(w_ufo);
    fclose(w_disapp);
    fclose(w_others);

    return 0;
}

我想问题出在strstr(),请告诉我问题所在。

问题出在
fscanf
中。您可能想要:

while (fscanf(r_in, "%79[^\n]\n", line) == 1)
                         ^
或者像约阿希姆·皮勒伯格评论的那样,只使用fgets

while (fgets(line, sizeof(line), r_in))

问题出在
fscanf
中。您可能想要:

while (fscanf(r_in, "%79[^\n]\n", line) == 1)
                         ^
或者像约阿希姆·皮勒伯格评论的那样,只使用fgets

while (fgets(line, sizeof(line), r_in))

如果你想阅读整行文字,你应该使用
fgets
。如果你想阅读整行文字,你应该使用
fgets