简体   繁体   中英

how can i read last 2 bytes from file and delete them after read

Here i developed on program for CRC 16 for file verification

Here i calculated CRC 16 of file and write this CRC value at the end of file. crc value data type is unsigned short so its take 2 bytes.

Code is here for that

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);
    }

Now i have to read these last two bytes from the file and want to delete them after read and again want to calculate CRC. so how can i read these last two bytes and delete them after read.

如果我理解正确,您想将文件rewind两个字节。

fseek(readFile, -2, SEEK_CUR);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM