用C更改.bin文件的数据 [英] Changing .bin file's data in C

查看:203
本文介绍了用C更改.bin文件的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多存储仓格式结构的序列数据。我希望能够随机读取任何的结构,并修改它在C.我用下面的code尝试,但它不工作。有人可以帮我吗?

此外,才有可能以删除之间文件的中间结构?

在code是如下:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;结构REC {
        INT X,Y,Z;
};无效f_rite()
{
        INT I;
        FILE * ptr_myfile;
        结构体REC my_record;        ptr_myfile =的fopen(TEST.bin,烧写,W);        对于(I = 0; I&小于5;我++){
                my_record.x = I;
                FWRITE(安培; my_record,的sizeof(结构REC),1,ptr_myfile);
        }        FCLOSE(ptr_myfile);        返回;
}
无效f_read()
{
        INT I;
        FILE * ptr_myfile;
        结构体REC my_record;        ptr_myfile = FOPEN(TEST.bin,烧写,R);        对于(i = 1; I< = 5;我++){
                FREAD(安培; my_record,的sizeof(结构REC),1,ptr_myfile);
                的printf(%d个\\ N,my_record.x);
        }
        的printf(\\ n);        FCLOSE(ptr_myfile);        返回;
}无效f_rerite()
{
        INT I;
        FILE * ptr_myfile;
        结构体REC my_record;        ptr_myfile = FOPEN(TEST.bin,烧写,RW);        为(ⅰ= 5; I> = 0;我 - ){
                fseek的(ptr_myfile,sizeof的(结构拍摄)*我,SEEK_SET);
                FREAD(安培; my_record,的sizeof(结构REC),1,ptr_myfile);
                my_record.x = my_record.x + 100;
                FWRITE(安培; my_record,的sizeof(结构REC),1,ptr_myfile);
        }        FCLOSE(ptr_myfile);        返回;
}诠释的main()
{
        f_rite();
        f_read();
        f_rerite();
        f_read();        返回0;
}


解决方案

有没有RW标志给fopen。你需要R +用于读取和写入(更新)。由于它的二进制数据,就应该实际使用在你f_read功能在f_rite功能R + B和WB和RB。
另外:


  • 检查调用的返回值可能会失败,你会发现,例如FWRITE失败。

  • 您f_rerite功能遍历6个元素,你一个是关闭。

  • 您f_rerite也写入下一个元素。可能要更新,而当前记录。这意味着你需要调用FREAD后再次FSEEK。

I have a lot of data stored in bin format as a sequence of structs. I want to be able to read randomly any of the structs and modify it in C. I am trying with the following code but it doesn't work. Can someone fix it for me please?

Also, would it be possible to delete an intermediate struct from the file in between?

The code is below:

#include <stdio.h>
#include <stdlib.h>

struct rec {
        int x,y,z;
};

void f_rite()
{
        int i;
        FILE *ptr_myfile;
        struct rec my_record;

        ptr_myfile=fopen("test.bin","w");

        for ( i=0; i < 5; i++ ) {
                my_record.x = i;
                fwrite( &my_record, sizeof(struct rec), 1, ptr_myfile );
        }

        fclose(ptr_myfile);

        return;
}


void f_read()
{
        int i;
        FILE *ptr_myfile;
        struct rec my_record;

        ptr_myfile=fopen("test.bin","r");

        for ( i=1; i <= 5; i++) {
                fread(&my_record,sizeof(struct rec),1,ptr_myfile);
                printf("%d\n",my_record.x);
        }
        printf("\n");

        fclose(ptr_myfile);

        return;
}

void f_rerite()
{
        int i;
        FILE *ptr_myfile;
        struct rec my_record;

        ptr_myfile=fopen("test.bin","rw");

        for ( i=5; i >= 0; i-- ) {
                fseek( ptr_myfile, sizeof(struct rec)*i, SEEK_SET );
                fread( &my_record, sizeof(struct rec), 1, ptr_myfile );
                my_record.x = my_record.x + 100;
                fwrite( &my_record, sizeof(struct rec), 1, ptr_myfile );
        }

        fclose(ptr_myfile);

        return;
}

int main()
{
        f_rite();
        f_read();
        f_rerite();
        f_read();

        return 0;
}

解决方案

There is no "rw" flag to fopen. You need "r+" for reading and writing(updating). Since it's binary data, you should actually use "r+b" , and "wb" in your f_rite function and "rb" in your f_read function. Also:

  • Check the return value of calls that could fail, you'd discover that e.g. fwrite failed.
  • Your f_rerite functions iterates through 6 elements, you're off by one.
  • Your f_rerite also writes to the next element. Likely you want to update the current record rather. Which means you need to fseek again after calling fread.

这篇关于用C更改.bin文件的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆