这里我开发了用于CRC 16
的文件验证程序
这里我计算了文件的 CRC 16 并将此 CRC
值写入文件末尾。 crc 值数据类型是 unsigned short
所以它占用 2 个字节。
代码在这里
void appendCRCtoFile(const char* filePath, unsigned short result) {
FILE *readFile;
//open a file for Reading
readFile = fopen(filePath, "ab");
fseek(readFile, SEEK_END, SEEK_SET);
const unsigned char check_bytes[2] = { result >> 8, result & 255 };
const size_t wrote = fwrite(check_bytes, 1, sizeof(check_bytes), readFile);
if (wrote == 2) {
printf("succesfull wrote 2 bytes\n");
} else {
printf("Failed to wrote 2 bytes\n");
}
fclose(readFile);
}
现在我必须从文件中读取最后两个字节并希望在读取后删除它们并再次计算 CRC。那么我如何读取这最后两个字节并在读取后删除它们。
最佳答案
如果我没理解错的话,你想回退
文件两个字节。
fseek(readFile, -2, SEEK_CUR);
关于c - 如何从文件中读取最后 2 个字节并在读取后删除它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9600083/