Parsing a binary file. What is a modern way?

后端 未结 10 2025
悲哀的现实
悲哀的现实 2021-01-30 01:41

I have a binary file with some layout I know. For example let format be like this:

  • 2 bytes (unsigned short) - length of a string
  • 5 bytes (5 x chars) - the
10条回答
  •  不思量自难忘°
    2021-01-30 02:24

    I personally do it this way:

    // some code which loads the file in memory
    #pragma pack(push, 1)
    struct someFile { int a, b, c; char d[0xEF]; };
    #pragma pack(pop)
    
    someFile* f = (someFile*) (file_in_memory);
    int filePropertyA = f->a;
    

    Very effective way for fixed-size structs at the start of the file.

提交回复
热议问题