1
일부 문자열을 분류하기 위해 C 프로그램을 작성했습니다. FILE 스트림을 사용하여 파일을 읽고 씁니다. 그러나 나는 문제를 발견했다. 일반적으로 문자열을 분류해야하지만 그렇지 않습니다. 이 코드는 괜찮아 문제를 찾을 수 없다고 생각합니다. 도와주세요.이 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;
}
소스 파일 "spooky.csv"
30.685163,-68.137207,Type=Yeti
28.304380,-74.575195,Type=UFO
29.132971,-71.136475,Type=Ship
28.343065,-62.753906,Type=Elvis
27.868217,-68.005371,Type=Goatsucker
30.496017,-73.333740,Type=Disappearance
26.224447,-71.477051,Type=UFO
29.401320,-66.027832,Type=Ship
37.879536,-69.477539,Type=Elvis
22.705256,-68.192139,Type=Elvis
27.166695,-87.484131,Type=Elvis
나는 문제가 않는 strstr() 문제에 대해 알려주세요 생각합니다.
전체 행을 읽으려면 대신'fgets'을 사용해야합니다. –