如何使用“ 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 (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...