如何在文件中搜索新输入​​中的匹配项?

时间:2019-03-20 08:35:17

标签: c file match

我是C语言的新手,我需要帮助。
程序将询问用户帐号和帐户密码。然后将搜索文件“ Account.txt”。如果帐号和密码与写在文件上的内容匹配,则会显示另一个选项,例如余额 查询存款,< strong>提款和退出
如何在文件中搜索匹配项?我该怎么办?

以下是一些代码:

void OpenAnAccount() {
  FILE * fptr;
  fptr = fopen("Account.txt", "w");

  if (fptr == NULL) {
    printf("File does not exists \n");
  }

  printf("*************************************\n\n");
  printf("\n ACCOUNT CREATION ");

  printf("\n**********************************\n");
  printf("Enter Your Account Number :\n");
  scanf("%d", & acctno);
  printf("Enter your Name:\n");
  scanf("%s", & acctname);
  printf("Enter your Account Pin: \n");
  scanf("%d", & acctpin);
  printf("Please Enter Your Initial Deposit :");
  scanf("%f", & dep);
  fprintf(fptr, "%d%s%d%f\n", acctno, acctname,
    acctpin);
}

void BankTransactions() {
  int AN, AP;
  printf("Enter 5 digits Account Number :\n");
  scanf("%d", & AN);
  printf("Enter 4 digits Accoount Pin :\n");
  scanf("%d", & AP);
}

1 个答案:

答案 0 :(得分:0)

存储帐号 pin ,并在每行中用逗号(,)和单个条目隔开。

  • acount.txt

4654654688481,1234

char *account,*pin;
FILE *myfile = fopen ( filename, "r" );     
if(myfile)
{ 
      char entry [100]; //maximum line size 
      while(fgets(entry,sizeof(entry),myfile)!= NULL)
      {
        account=strtok(entry,",");
        pin = strtok(NULL,",");
        //match these with user input if matches break out of loop
      }
      fclose (myfile);
}

您可以执行以下操作。您将可以检索“帐户”和“固定”