我想寫一個程序,可以搜索文件中的字符串(稱爲student.txt)。我希望我的程序在文件中找到相同的單詞時打印該單詞,但它顯示錯誤。在c中的文件中搜索字符串
#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;
}
如果(!STRCMP(字符串,學生))== 0應該如果(!STRCMP(字符串,學生)== 0) – Anshul 2015-04-03 09:32:43
仍然得到錯誤 – jimo 2015-04-03 10:17:38
你得到什麼樣的錯誤被替換究竟? – mushfek0001 2015-04-03 11:08:20