我需要逐行搜索文件中的两个特定单词,如果它们存在,则打印“找到!”。
这是 file.txt(有四列)
bill gates 62bill microsoft
beyonce knowles 300mill entertainment
my name -$9000 student
以下是我的想法,但它似乎不起作用
char firstname[];
char lastname[];
char string_0[256];
file = fopen("file.txt","r+");
while((fgets(string_0,256,file)) != NULL) {
//scans the line then sets 1st and 2nd word to those variables
fscanf(file,"%s %s",&firstname, &lastname);
if(strcmp(firstname,"beyonce")==0 && strcmp(lastname,"knowles")==0){
printf("A match has been found");
}
}
fclose(file);
请帮忙。可能是指针没有移动到while循环中的下一行吗?如果是这样,我该如何解决?