Reading Binary File into a Structure (C++)

后端 未结 5 860
遇见更好的自我
遇见更好的自我 2021-02-04 15:42

So I\'m having a bit of an issue of not being able to properly read a binary file into my structure. The structure is this:

struct Student
{
    char name[25];
         


        
5条回答
  •  被撕碎了的回忆
    2021-02-04 16:21

    A simple workaround is to pack your structure to 1 byte

    using gcc

    struct __attribute__((packed)) Student
    {
        char name[25];
        int quiz1;
        int quiz2;
        int quiz3;
    };
    

    using msvc

    #pragma pack(push, 1) //set padding to 1 byte, saves previous value
    struct  Student
    {
        char name[25];
        int quiz1;
        int quiz2;
        int quiz3;
    };
    #pragma pack(pop) //restore previous pack value
    

    EDIT : As user ahans states : pragma pack is supported by gcc since version 2.7.2.3 (released in 1997) so it seems safe to use pragma pack as the only packed notation if you are targetting msvc and gcc

提交回复
热议问题