Sign in to follow this  

C++ how to directly read into a struct of struct

This topic is 3627 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello, i have this struct definition from angelcodes BMfont:
#pragma pack(1)
struct charsBlock
{
    struct charInfo
    {
        unsigned short  id;
        unsigned short  x;
        unsigned short  y;
        unsigned short  width;
        unsigned short  height;
        short xoffset;
        short yoffset;
        short xadvance;
        unsigned char  page;
        unsigned char  chnl;
    } chars[1];
};

i know how to read into a struct: fstream.read((char*) &pagesb, size); but in this case it is a struct that contains another struct. how can i read into that? also, what does #pragma pack(1) mean btw. thanks a lot!

Share this post


Link to post
Share on other sites
Quote:
Original post by ehmdjii
i know how to read into a struct: fstream.read((char*) &pagesb, size);


This actually reads data into a POD type. A struct is not always a POD type, and a POD type can be something other than a struct (such as a base type or a class).

Quote:
but in this case it is a struct that contains another struct. how can i read into that?


The struct which contains a struct is also a POD, and it also has the same layout as the contained structure (since the only contents of that struct is an array of the inner one). Therefore, you can read it from file in the same way.

Quote:
also, what does #pragma pack(1) mean btw.


This asks the compiler not to place invisible padding data in-between fields or at the end of structures, so that the size of the structure is equal to the sum of sizes of its fields.

Share this post


Link to post
Share on other sites
Quote:
Original post by ehmdjii
i know how to read into a struct: fstream.read((char*) &pagesb, size);


Be careful; this isn't portable. Compilers can add padding between elements of a struct. The amount of padding may vary from compiler to compiler. So saving this struct with a GCC-compiled binary on a Linux machine and reading it with an MSVC-compiled binary on an Win32 machine might not work as they might have different padding conventions. This may not be a concern if you're only supporting a single system.

Quote:

also, what does #pragma pack(1) mean btw.


This specifies the alignment for the structure. This directive isn't supported by all compilers, but those that do support it will make sure that the elements of the structure have no padding between them. This is to allow the kind of fread() trick you mention in as many cases as possible because the layout of the structure in memory and on disk will be consistent across more platforms (assuming the sizes of the primitive types are the same on those platforms).

Share this post


Link to post
Share on other sites
thanks a lot!
so POD is plain old data, right?


ok, i still did not fully understand how to access this inner struct. this is what i try:


charsb.chars = new charInfo[numChars];
for (int i=0; i<numChars; i++) {
f.read((char*) charsb[i].chars, sizeof(charsb->chars));
}



but it cant allocate the array.

Share this post


Link to post
Share on other sites
Read up on how arrays work in C++. However, as a quick refresher, remember that C++ arrays do not need to be initialized—creating an array variable automatically creates the corresponding array, which can be accessed for the rest of the lifetime of that variable. So, the chars[1] variable creates a one-value array.

If your intent is to use a dynamic-sized array, C++ provides you with std::vector. This container provides you with the sequentiality guarantees that you need to be able to read a buffer of PODs all at once.

Share this post


Link to post
Share on other sites

This topic is 3627 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this