gpt-3.5-turbo-0301· OpenAI WildChat-4.8M (AI2)
C语言实现一组读写文件的工具接口,可以按bit读写, 按byte读写 ,变长读写
- User
C语言实现一组读写文件的工具接口,可以按bit读写, 按byte读写 ,变长读写
- Assistant
- 按 bit 读写文件:
// 读取文件中指定位置的 bit 值 int readBit(FILE *fp, long bitPos);
// 在文件中指定位置写入 bit 值 void writeBit(FILE *fp, long bitPos, int bitValue);
- 按 byte 读写文件:
// 读取文件中指定位置的 byte 值 unsigned char readByte(FILE *fp, long bytePos);
// 在文件中指定位置写入 byte 值 void writeByte(FILE *fp, long bytePos, unsigned char byteValue);
- 变长读写文件:
// 读取变长编码的整型数值 int readVarInt(FILE *fp, long startBitPos, long *endBitPos);
// 在指定位置写入变长编码的整型数值 void writeVarInt(FILE *fp, long startBitPos, int value, long *endBitPos);
// 读取任意长度的二进制数据 char *readBytes(FILE *fp, long startPos, long length);
// 在指定位置写入任意长度的二进制数据 void writeBytes(FILE *fp, long startPos, char *data, long length);
- User
实现读写文件接口
- Assistant
以下是 C 语言实现读写文件接口的代码:
#include <stdio.h> #include <stdlib.h> // 读取文件中指定位置的 bit 值 int readBit(FILE *fp, long bitPos) { fseek(fp, bitPos / 8, SEEK_SET); // 定位到指定的字节位置 return ((fgetc(fp) & (0x80 >> (bitPos % 8))) > 0); } // 在文件中指定位置写入 bit 值 void writeBit(FILE *fp, long bitPos, int bitValue) { fseek(fp, bitPos / 8, SEEK_SET); unsigned char ch = fgetc(fp); if (bitValue) { ch |= (0x80 >> (bitPos % 8)); // 设置为 1 } else { ch &= ~(0x80 >> (bitPos % 8)); // 设置为 0 } fseek(fp, -1, SEEK_CUR); // 回退到当前位置 fputc(ch, fp); } // 读取文件中指定位置的 byte 值 unsigned char readByte(FILE *fp, long bytePos) { fseek(fp, bytePos, SEEK_SET); // 定位到指定的字节位置 return fgetc(fp); } // 在文件中指定位置写入 byte 值 void writeByte(FILE *fp, long bytePos, unsigned char byteValue) { fseek(fp, bytePos, SEEK_SET); fputc(byteValue, fp); } // 读取变长编码的整型数值 int readVarInt(FILE *fp, long startBitPos, long *endBitPos) { int value = 0; int shift = 0; long bitPos = startBitPos; int b; do { b = readBit(fp, bitPos++); value |= (b & 0x7f) << shift; shift += 7; } while (b & 0x80); *endBitPos = bitPos - 1; // 记录结束位置 return value; } // 在指定位置写入变长编码的整型数值 void writeVarInt(FILE *fp, long startBitPos, int value, long *endBitPos) { long bitPos = startBitPos; int b; do { b = value & 0x7f; value >>= 7; if (value) { b |= 0x80; } writeBit(fp, bitPos++, b); } while (value); *endBitPos = bitPos - 1; // 记录结束位置 } // 读取任意长度的二进制数据 char *readBytes(FILE *fp, long startPos, long length) { char *buf = (char *)malloc(length); fseek(fp, startPos, SEEK_SET); fread(buf, 1, length, fp); return buf; } // 在指定位置写入任意长度的二进制数据 void writeBytes(FILE *fp, long startPos, char *data, long length) { fseek(fp, startPos, SEEK_SET); fwrite(data, 1, length, fp); }这些接口可以直接使用,例如:
int main() { FILE *fp = fopen("test.txt", "wb+"); // 读写 bit writeBit(fp, 3, 1); int bit = readBit(fp, 3); printf("bit = %d\n", bit); // 读写 byte writeByte(fp, 6, 'A'); unsigned char byte = readByte(fp, 6); printf("byte = %c\n", byte); // 读写变长编码的整型数值 long endPos; writeVarInt(fp, 9, 123456, &endPos); int num = readVarInt(fp, 9, &endPos); printf("num = %d\n", num); // 读写任意长度的二进制数据 char *data = "Hello, world!"; writeBytes(fp, 100, data, 13); char *buf = readBytes(fp, 100, 13); printf("buf = %s\n", buf); fclose(fp); free(buf); return 0; }