0
int main (void){
    do
    {
        mainmenu:
        mainmenu();
        switch(option)
        {
                //add new contact
                case 1:addcontact(storage);savecontact(storage);
                    break;
                case 2:searchcontact();
            break;
        }
    }while(true);
}
void searchcontact()
{
    char searchname[20];
    system("cls");
    do
    {
        int find = 0;
        printf("Contact Search\n Name of the Contact:\n");
        fflush(stdin);
        scanf("%[^\n]",&searchname);
        length=strlen(searchname);
        f=fopen("contact.txt","rb");

        system("cls");
        printf("Search the result for %s\n",searchname);
        while(fread(&storage,sizeof(storage),space,f)==true)
        {
            for(i=0;i<=length;i++)
            storage[space].name[i]=storage[space].name[i];
            storage[space].name[length]='\0';
            if(stricmp(storage[space].name,searchname)==false)
                printf("Name\t:%s\nPhone\t:%d\nE-mail\t:%s\n",storage[space].name,storage[space].hpnum,storage[space].email);
                find++;
        }
        if(find==false)
            printf("\nNo match found!");
            else
            printf("\n %d match(s) found",find);
            fclose(f);
            printf("\nTry again?\t[1] Yes\t[2] No\n");
            scanf("%d",&choice);
    }while(choice==true);
}

我在搜索时遇到问题...添加联系人后,我无法再次搜索...我使用 stricmp 搜索联系人...同时我输入了我为保存联系人而输入的正确名称,它仍然无法搜索...继续打电话给我再试一次。因此,该错误主要存在于那里的搜索功能上。我的真值是==1,我的假值是==0。

4

1 回答 1

0

fread 返回许多已读项目(不是 true 或 false ...)。

  • 然后在您的情况下,仅当数据库中只有一项时它才返回 1。
  • searchname 是一个 char 数组,所以 searchname 是 char * (&searchname is char * *)

对不起代码,目前我无法正确地将它传递给 SO,它不想粘贴 c 缩进代码。我可以邮寄给你。

void searchcontact()
{
  char searchname[20],name[20];
  clearscreen();
  do {
      int i=0;
      int find = 0;
      int found = -1;
      int local_index=i%space;
      printf("Contact Search\n Name of the Contact:\n");
      fflush(stdin);
      //        scanf("%[^\n]",searchname);
      scanf("%s",searchname);
      length=strlen(searchname);
      f=fopen("contact.txt","rb");
      clearscreen();
      printf("Search the result for %s\n",searchname);
      while ( fread(&storage[local_index],sizeof(struct contact),1,f)==1 )
    {
      if(stricmp(storage[i%space].name,searchname)==false)
        {    
          printf("Name\t:%s\nPhone\t:%d\nE-mail\t:%s\n",storage[local_index].name,storage[local_index].hpnum,storage[local_index].email);
          find++;
          found=i;
        }
      i++;
      local_index=i%space;
    }
      if(find==false) printf("\nNo match found!");
      else printf("\n %d match(s) found",find);
      fclose(f);
      printf("\nTry again?\t[1] Yes\t[2] No\n");
      scanf("%d",&choice);
    } while(choice==true);
}
于 2013-03-03T20:53:18.227 回答