C
バリナリ

cでバイナリ一部書き換え

#include <stdio.h>

typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned long ulong;


ulong readBin(ulong s, char fname[]){
    FILE *fp;
    uchar p_temp_data;
    ushort ret;

    fp=fopen(fname, "rb");

    while(s--){
        p_temp_data=getc(fp);
        printf("0x%x\t", p_temp_data);
    }

    ret=(ushort)0x12345678;

    fclose(fp);

    return ret;
}

void rewriteBin(ushort crc,char fname[]){
    uchar address=0x1c;
    FILE *fp;

    fp=fopen(fname,"rb+");

    fseek(fp,address,SEEK_SET);
    fwrite(&crc, sizeof(crc),1,fp);
    fclose(fp);
}

int main(void){
    ushort crc=0;
    char fname[]="bf_bak.bin";
    char newfname[]="bf.bin";
    crc=readBin(0x1c,fname);
    printf("\n\n");
    rewriteBin(crc,newfname);

    //確認用
    crc=readBin(0x20,newfname);
    printf("\n");
    return 0;
}