String 联系人系统管理的搜索功能问题 int main(无效){ 做 { 主菜单: 主菜单(); 开关(选件) { //添加新联系人 案例1:addcontact(存储);savecontact(存储); 打破 案例2:searchcontact(); 打破 } }虽然(正确); } 无效搜索联系人() { 字符搜索名称[20]; 系统(“cls”); 做 { int find=0; printf(“联系人搜索\n联系人名称:\n”); fflush(stdin); scanf(“%[^\n]”,&searchname); 长度=strlen(搜索名称); f=fopen(“contact.txt”、“rb”); 系统(“cls”); printf(“搜索结果以查找%s\n”,searchname); while(fread(&storage,sizeof(storage),space,f)==true) { for(i=0;i
fread返回大量已读项目(非真或假…)。String 联系人系统管理的搜索功能问题 int main(无效){ 做 { 主菜单: 主菜单(); 开关(选件) { //添加新联系人 案例1:addcontact(存储);savecontact(存储); 打破 案例2:searchcontact(); 打破 } }虽然(正确); } 无效搜索联系人() { 字符搜索名称[20]; 系统(“cls”); 做 { int find=0; printf(“联系人搜索\n联系人名称:\n”); fflush(stdin); scanf(“%[^\n]”,&searchname); 长度=strlen(搜索名称); f=fopen(“contact.txt”、“rb”); 系统(“cls”); printf(“搜索结果以查找%s\n”,searchname); while(fread(&storage,sizeof(storage),space,f)==true) { for(i=0;i,string,function,search,compare,String,Function,Search,Compare,fread返回大量已读项目(非真或假…)。 在您的例子中,只有当数据库中只有一个项目时,它才会返回1 searchname是字符数组,因此searchname是char*(&searchname是char**) 代码很抱歉,目前我无法正确地通过它,它不想粘贴c缩进代码。我可以将它邮寄给您 int main (void){ do { mainmenu: mainmenu(); switch(option) {
- 在您的例子中,只有当数据库中只有一个项目时,它才会返回1
- searchname是字符数组,因此searchname是char*(&searchname是char**)
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);
}
是C代码吗?有些函数和变量定义缺失,比如选项存储选项…你能给出缺失的部分吗?@nullix没关系…我正试图将我的代码上传到其他地方,你可以下载它…=)因为这里不能写太多的代码,我更希望你下载我的文件并理解我的整个代码,以便让你解决我的问题。=)我将它移植到linux进行测试,并修复了你的函数请参见答案。这对连接一次、创建一个配置文件并不再阅读答案是很不友好的。人们花了几个小时阅读你的精彩代码.我回答。
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);
}