c_cpp 在C中读写二进制数据块
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 在C中读写二进制数据块相关的知识,希望对你有一定的参考价值。
#include <stdio.h>
#include <stdlib.h>
#define ASC_SIZE 128
#define BLOCK_SIZE (128 * 9)
typedef struct
{
unsigned char data[BLOCK_SIZE];
} RootBlock;
// Gets offsetted file node from block
unsigned long
RootBlock_get_pos(RootBlock* rb, long index)
{
unsigned long* positions = (unsigned long*)(rb->data + ASC_SIZE);
return positions[index];
}
int main(void) {
FILE * pFile;
unsigned long locations[ASC_SIZE] = {0};
unsigned char states[ASC_SIZE] = {0};
unsigned char beg[2] = {22, 33};
pFile = fopen ("myfile.roots", "a+b");
fwrite(beg, sizeof(unsigned char), sizeof(beg), pFile);
fwrite (locations , sizeof(unsigned long), ASC_SIZE, pFile);
fwrite (states , sizeof(unsigned char), ASC_SIZE, pFile);
fclose (pFile);
pFile = fopen ("myfile.roots", "rb");
unsigned char beg_r[2];
fread(beg_r, sizeof(unsigned char), 2, pFile);
printf("The beginning bytes are %u and %u\n", beg_r[0], beg_r[1]);
return 0;
}
以上是关于c_cpp 在C中读写二进制数据块的主要内容,如果未能解决你的问题,请参考以下文章
c_cpp C语言二进制文件读写
c_cpp 读写二进制文件的.cpp
c_cpp Duff的设备 - 在C中快速复制块
c_cpp 用C读写文件
c_cpp 将二进制数据写入c中的文件
c_cpp 读写