Reading binary file defined by a struct

后端 未结 5 1778
轻奢々
轻奢々 2021-02-11 01:23

Could somebody point me in the right direction of how I could read a binary file that is defined by a C struct? It has a few #define inside of the struct, which makes me thing t

5条回答
  •  长发绾君心
    2021-02-11 02:01

    You can also use unions to do this parsing if you have the data you want to parse already in memory.

    union A {
        char* buffer;
        Format format;
    };
    
    A a;
    a.buffer = stuff_you_want_to_parse;
    
    // You can now access the members of the struct through the union.
    if (a.format.str_name == "...")
        // do stuff
    

    Also remember that long could be different sizes on different platforms. If you are depending on long being a certain size, consider using the types defined int stdint.h such as uint32_t.

提交回复
热议问题