代码之家  ›  专栏  ›  技术社区  ›  Ofek Pintok

使用c中的fseek进行文件定位,无法正确工作

  •  0
  • Ofek Pintok  · 技术社区  · 7 年前

    所以基本上,我想:

    1. 读取行并将数据插入局部变量
    2. 移回薪资项目的开头(在文件中)。

    123, Joe, 1200

     void updateSalary(char* filename)
     {
        employee temp;
        char c;
        float increase;
        FILE *fup = fopen(filename, "r+");
        while ((c=fgetc(fup)) != EOF)
        {
        fscanf(fup, "%*d%s%f", &temp.name, &temp.salary);
            printf("How big is the increase to %s's salary?\n", temp.name);
            scanf("%f", &increase);
            while ((c=fgetc(fup)) != ' ')
                fseek(fup, -1, SEEK_CUR);
            fprintf(fup, "%f", temp.salary + increase);
            fseek(fup, 1, SEEK_CUR);
        }
     fclose(fup);
    }
    

    如何覆盖新的薪资:我使用while循环查找薪资数据之前出现的退格,同时向后移动每个字节并查找它。工作不正常。其次,在打印新数据之后,为下一个字符设置文件位置( '\n' )我想跳过这一步,所以我用了1字节的fseek。也没用。

    谢谢!

    0 回复  |  直到 7 年前