c++cbinarysequentialfseek

Read len bytes of a file at offset and write it to FILETIME


Is there any possibility to read sth from a binary file when knowing only the size of the element that should be red and the offset (in hexa)?

For example, if I want to read a FILETIME variable of 8 bytes, that has offset 0x001C, how do I approach this is C or C++?

I tried

fseek(pFile, 0x001C, SEEK_SET);
FILETIME* temp =(FILETIME*) malloc(sizeof(FILETIME));
fscanf(pFile, "%d", *temp);

but it doesn't properly work, I don't know why.

Thanks


Solution

  • You can set your offset with fseek, and then read len of bytes with fread:

    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
        FILE *file = fopen("read_bytes.c", "rb");
        size_t offset = 0x001C;
        size_t len = 8;
    
        fseek(file, offset, SEEK_SET);
        FILETIME* temp = malloc(sizeof(FILETIME));
        fread(temp, 1, len, file);
    }