Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 为什么不';插入的用户值是否与文件值匹配?_C_File - Fatal编程技术网

C 为什么不';插入的用户值是否与文件值匹配?

C 为什么不';插入的用户值是否与文件值匹配?,c,file,C,File,最近我一直在做一个项目,这个项目给了我一个文件,其中包含一个16位数的整数,后面是一个4位数的整数(卡号和密码)。我的目标是创建一个用户密码系统,在3次登录尝试失败后,该系统将关闭。现在,这不是问题,我的问题是,无论我如何编写代码,我的输入(无论是卡号还是密码)都与文件值不匹配。这是我的密码 typedef struct{ int number[16]; int pass[4]; } TypeCard; int main(void) { int i, c = 0, n, p,

最近我一直在做一个项目,这个项目给了我一个文件,其中包含一个16位数的整数,后面是一个4位数的整数(卡号和密码)。我的目标是创建一个用户密码系统,在3次登录尝试失败后,该系统将关闭。现在,这不是问题,我的问题是,无论我如何编写代码,我的输入(无论是卡号还是密码)都与文件值不匹配。这是我的密码

typedef struct{
   int number[16];
   int pass[4];
} TypeCard;

int main(void)
{
   int i, c = 0, n, p, cards;

   TypeCard card;

   FILE *f = fopen("cards.txt", "r");

   for(i = 0; fscanf(f, "%d %d", card.number, card.pass)!= EOF; i++)
   {
      cards++; /*this is merely to fill this loop, i don't know if ill need it*/
   }

   fclose(f);

   while(c != 3)  /*c is a counter */
   {
      int al = 2 - c;  /*al = attempts left*/

      printf("Insert card number: ");
      scanf("%d", &n);

      if(n == card.number[i])
      { 
          printf("Insert password: ");
          scanf("%d", &p);

          if(p == card.pass[i])
          {
              printf("Access granted\n");
              return 0;
          }
          else
          {
              printf("Wrong password\n");
              printf("Attempts left: %d\n", al);
              c++;
          }
      }
      else
      {
          printf("Invalid card\n");
          printf("Attempts left: %d\n", al);
          printf("Try again.\n\n");
          c++;
      } 
  }

  printf("Access blocked. Please contact the system administrator.\n");

  return 0;
}

有人能帮我吗?

您正在尝试将一个16位数字读入一个整数,该整数最多32位,只能处理10位数字。要处理16位数字,您需要使用“long long”,它是64位,可以处理20位数字。

循环的每次迭代-您认为这是在做什么
fscanf(f,“%d%d”,card.number,card.pass)
-我想这不是你的intention@EdHeal这不是应该将文件中的int值存储到结构的成员中吗?请尝试数组的第一个成员!我猜你想填满阵列。您还需要检查绑定。你也可以有一个空循环,所以
cards++不可用required@EdHeal我没有,没有任何办法,无论是最初的成员还是最后的成员。我只是尝试将struct元素设置为整数,并适当地调整代码,但即使这样也无济于事。至于循环部分,谢谢,我想如果循环是空的,函数就不能工作。检查您是否已打开文件(即
f
未满)。2.学习使用调试器