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

标签 c file struct

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

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

这是我对该程序的(我猜是不正确的)方法:

(它应该如何工作:输入: james ,3.输出:Match)

 *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 编译器。

最佳答案

打字错误:

*FILE *fp;

应该是:

FILE *fp;

scanf 的参数预计是指针,但 string 已经(衰减为)指针。

scanf("%s",&string);

应该是:

scanf("%s",string);

关于C:使用结构比较文件中的 2 个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21430481/

相关文章:

c - 在适当的位置添加一个节点(列表将按升序排列)

c - 如何在 LattePanda 上启用 ACPI(用于关机)

python - 如何检查文件夹中的文件是否与 .csv 文件中指定的文件名一致?

php - 拒绝访问 htaccess 中的多个文件

java - 如何正确地将 .Text 属性绑定(bind)到 SimpleObjectProperty 的 Overridden .asString() 方法?

go - 将数据添加到结构中的 interface{}

c - 运行时导出 C API 限制

c++ - 什么是无锁多线程编程?

c - 识别gtk中滚动条的向上/向下移动

c++ - HEAP 错误 指定给 RtlValidateHeap 的地址无效