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つしかない場合にのみ1を返します。
  • searchname は char の配列なので、searchname は char * (&searchname は 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 に答える