如何解决如何使用“ fseek”正确搜索文件中的每个记录
我正在该程序中工作,我在寻找和覆盖一部分数据方面遇到困难。
我将学生存储在ab mode
中。当我在存储的文件中打印数据时,它们看起来不错。但是当我想使用fseek()
查找数据时,出现了问题。
在这种情况下,我想通过ID查找一名学生并替换其ID。问题是我只能替换第一人称数据并为其提供资金。其他结果就像它们不存在一样。
void change_id()
{
int long pos;
int id_temp;
int new_id;
FILE *ftemp;
ftemp=fopen(file_person,"rb+");
if(ftemp==NULL)
{
printf("\nError in file!");
return;
}
while(fread(&nr1[nr_person],sizeof(struct person),1,ftemp))
{
printf("\nGive the person Id:");
fflush(stdin);
scanf("%d",&id_temp);
rewind(ftemp);
fseek(ftemp,sizeof(struct person)*(id_temp-1),SEEK_SET);
if(id_temp==nr1[nr_person].id)
{
printf("\nGive the new ID:");
scanf("%d",&new_id);
nr1[nr_person].id=new_id;
fwrite(&nr1[nr_person],ftemp);
break;
}
else {
printf("Id doesnt exist!");
return;
}
也许我没有正确存储学生。 这是我所做的:
struct person{ //This is the struct i have done
int id;
char name[20];
int pass;
}nr1[50];
int nr_person; //I was trying to use this like a student count
void register_person()
{ //I'm I using the number person wrong?
FILE *f;
f=fopen("person.txt","ab");
if(f==NULL){
printf("Error!");
return ;
}
nr_person++;
nr1[nr_person].id=read_data(file_person);
printf("Generated Id is %d",nr1[nr_person].id);
printf("\nName : ");
scanf("%s",nr1[nr_person].name);
nr1[nr_person].pass=generate_password(nr1[nr_person].id,nr1[nr_person].name);
fwrite(&nr1[nr_person],f);
if(fwrite!=0){
printf("\n\n Data saved");}
else printf("\nData not saved!");
fclose(f);
}
我一直在努力这部分。谁能帮我吗?
谢谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。