【发布时间】:2015-06-08 09:04:26
【问题描述】:
我正在尝试编写一个可以在文件(称为 student.txt)中搜索字符串的程序。我希望我的程序在文件中找到相同的单词时打印该单词,但显示错误。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
int num =0;
char word[2000];
char *string[50];
FILE *in_file = fopen("student.txt", "r");
//FILE *out_file = fopen("output.txt", "w");
if (in_file == NULL)
{
printf("Error file missing\n");
exit(-1);
}
while(student[0]!= '0')
{
printf("please enter a word(enter 0 to end)\n");
scanf("%s", student);
while(!feof(in_file))
{
fscanf(in_file,"%s", string);
if(!strcmp(string, student))==0//if match found
num++;
}
printf("we found the word %s in the file %d times\n",word,num );
num = 0;
}
return 0;
}
【问题讨论】:
-
if(!strcmp(string, student))==0 应替换为 if(!strcmp(string, student)==0)
-
仍然出现错误
-
你到底遇到了什么错误?
-
您是在寻找字符串搜索还是单词搜索。例如,如果您正在搜索字符串“to”。文件内容为:
。输出为 5。但实际上只有一个单词“to”
标签: c string search data-structures string-search