C:使用结构比较文件中的2个字符串

时间:2014-01-29 11:59:11

标签: c file struct

目前我正在学习结构和文件,我的任务是创建一个程序,它将获得一个字符串输入和一个数字输入。然后程序将继续打开一个随机数据库,找到要求的特定行,并检查字符串是否相等。 文本文件:

Billy
bob
james
peter
mike
kieran
obidiah
scarlett
john
chloe
sarah
leon
david
andrew
shawn
hannah
phoebe
chris
mark

这是我的(我在猜测,不正确)这个程序的方法:

(它应该如何工作:输入:james,3。输出:匹配)

 *FILE *fp;
 int main(void)
 {
   struct store
   {
     char def[128];
   }stock[10];

   int count;
   char string[64];

   printf("Enter the string: ");
   scanf("%s",&string);

   printf("Enter the line: ");
   scanf("%d",&count);

   fp=fopen("Names.txt","r");
   fscanf(fp,"%127[^\n]%*c", stock[count].def);
   if (strcmp(stock[count].def,string)==0)
   {
     printf("Match");
   }
   else
   {
     printf("Nope");
   }
   fclose(fp); 
   getch();
 }

这符合要求,但程序不会读取文件中的值。 任何人都知道如何使这个工作?

我使用windows和DEV C编译器。

1 个答案:

答案 0 :(得分:0)

错字:

*FILE *fp;

应该是:

FILE *fp;

scanf的参数应该是指针,但string已经(衰变成)指针。

scanf("%s",&string);

应该是:

scanf("%s",string);
相关问题