首页 > 解决方案 > 如何比较C程序文件中的字符串结构数组

问题描述

我正在为帐户交易编写一个项目。但是,在我的支票账户 ID 过程中存在一些小问题。我使用 struct 函数属于字符串数组,它可以帮助我稍后轻松解决我的子任务,重点是我尝试使用字符串比较检查 account_ID。输出无法显示我的期望。我不知道如何扫描数组中的变量以检查文件文本中的字符串数组结构。

我努力通过多种方式扫描变量。但它不起作用。这是我的代码。

struct customers{
char phone[13];
char id[9];
char name[31];
char address[201];
char city[31];
char date[11];}customer

int main()
{
   FIle *fc
   if((fc=fopen("clients.txt","a+"))=NULL)
        printf("Can not open");
   else
   {
        int i = 0, a = 0;
        fflush(stdin);
        customer_ID:
          printf("Enter the ID (maximum 8 digits): ";
          gets(customer[i].id);
        if (strlen(customer[i].id)!=8)
        {
            printf("Wrong the number of digits ! Enter Again!\n");
            goto customer_ID;
        } 
          while(fscanf(fc,"%s\n%[^\n]%*c%s\n%[^\n]%*c%[^\n]%*c%s\n\n",customer[a].id,customer[a].name,customer[a].phone,customer[a].address,customer[a].city,customer[a].date)!=EOF)
    {
        if (strcmp(customer[i].id,customer[a].id)==0)
        {
            printf("Account ID has been already used ! Please Enter other ID !\n");
            goto customer_ID;
        }
    }

输出:* 实际结果:它不打印“帐户 ID 已被使用!请输入其他 ID!”

   * Expected Result:
          Print "Account ID has been already used ! Please Enter other ID !"

标签: c

解决方案


也许尝试使用 strstr 以防 id 总是相同的大小,这样它就会

if (strstr(customer[i].id,customer[a].id))

这样你的持久代码空间


推荐阅读