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 :(得分:0)
fread返回一些读取的项目(不是真或假......)。
很抱歉代码,目前我无法正确地将它过去,它不想粘贴缩进代码。我可以邮寄给你。
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);
}