2013-03-02 88 views
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搜索联系人...尽管我发现了保存联系人的正确名称,但它仍然无法搜索出来......继续打电话给我再试一次。所以这个bug主要是在那里的搜索功能。我的真实值是== 1,而我的错误值是== 0。与联系人系统管理的搜索功能问题

+0

是C代码吗?一些功能和变量定义缺失,如选项存储选择...你可以给缺少的部分? – 2013-03-02 09:02:19

+0

@nullix这没关系......我试图将我的代码上传到其他地方,你可以下载它... =) 由于在这里不能写太多的编码,我宁愿你下载我的文件,并理解我的整个编码以便让您解决我的问题。 =) http://www.mediafire.com/?pkqsasr8forgjwv – jefferyleo 2013-03-02 12:21:53

+0

我把它移植到linux来测试它,并修复你的功能,看到答案。 – 2013-03-03 21:18:52

回答

0

fread返回一些读取项目(不是真或假......)。

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

对不起代码,目前我不能过去它SO正确,它并不要粘贴Ç缩进代码。我可以邮寄给你。

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); 
}