case 1:
printf("enter the name of the record to be updated\n");
scanf("%s",&buf1);
fseek(p1,sizeof(s),SEEK_SET);
while(fread(&s,sizeof(s),1,p1))
if(strcmp(buf1,s.name)==0)
{
printf("enter the accno,name,city,addr1,addr2,phno,accbal\n");
scanf("%s%s%s%s%s%s%d",s.accno,s.name,s.city,s.addr1,s.addr2,s.phno,&s.accbal);
fseek(p1,sizeof(s),SEEK_CUR);
fwrite(&s,sizeof(s),1,p1);
printf("the details are updated\n");
}
break;
- 我在switch case中编写了这段代码。无论何时执行此代码,都会形成循环。
- 如果在写入函数之后执行条件,则反复执行printf。我很困惑。有什么问题??
醇>
答案 0 :(得分:1)
问题可能是这些 -
scanf("%s",&buf1);
^remove this (assuming buf1 is a char array you used to store string)
而这 -
scanf("\n%s%s%s%s%s%s%d\n",&s.accno,&s.name,&s.city,&s.addr1,&s.addr2,&s.phno,&s.accbal);
移除&
s.name
s.city
前面的s.addr1
和类似内容。当你使用s.accbal
时,让它在%d
中,因此它可能是一个整数类型(因为你没有显示它们的声明)。
当你写这篇文章时break
在if
之外并且也循环。试试这个 -
while(fread(&s,sizeof(s),1,p1))
{
if(strcmp(buf1,s.name)==0)
{
printf("enter the accno,name,city,addr1,addr2,phno,accbal\n");
scanf("\n%s%s%s%s%s%s%d\n",&s.accno,&s.name,&s.city,&s.addr1,&s.addr2,&s.phno,&s.accbal);
fseek(p1,sizeof(s),SEEK_CUR);
fwrite(&s,sizeof(s),1,p1);
printf("the details are updated\n");
break; //break inside block of if statement
}
}