Jump to content
  • Advertisement
Sign in to follow this  
godmodder

Weird corruption problem with ifstream

This topic is 3665 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 want to read in the following struct from a binary .ms3d file:
// this is what the header looks like
struct MS3DHEADER
{
   char ID[10];
   int Version;
} Header;    // declare a header object so we can fill it in

So I write the following code to read the data:
File.read((char*)&Header, 14); // sizeof the struct is 14 bytes

This doens't work! The 10 characters get read in fine, but the data for the int is corrupt. When I read the components separately, it works fine:
File.read((char*)&Header.ID, 10);
File.read((char*)&Header.Version, 4);

Executing gcount() after both methods always reveals that 14 bytes are read from the file, so that leads me to believe that the data is somehow not being converted into the struct somehow. Is the casting method wrong perhaps? I couldn't find anything about this problem on the net. Jeroen

Share this post


Link to post
Share on other sites
Advertisement
Just an uneducated guess...

But I'm thinking it might be aligning that int to 12 bytes in.

So when you read in the first way... It reads in the first 10 bytes, 2 bytes of the int go to garbage padding, and the int gets the last two bytes. The second way alleviates that.

Quick check would be...

int distance = (int)((char*)(&Header.Version) - (char*)(&Header.ID)); If that's not 10, then you'll have a problem.

Share this post


Link to post
Share on other sites
Yes you probably shouldn't be making assumptions about the size of anything. That's what the sizeof operator is for:


File.read((char*)&Header, sizeof(Header));

Share this post


Link to post
Share on other sites
TtDTtW and visitor, you were right! The data for the chars was indeed getting aligned at 12 bytes. But when I read with the sizeof() operator, I'm actually reading in 16 bytes, which is obviously not what I want.

How can I resolve this?

Jeroen

Share this post


Link to post
Share on other sites
You have two options here:
1. Read the fields one at a time. This way the padding does not matter anymore.

2. Tell the compiler that the struct should not be padded.
With MSVC++ it works like this:

// Set alignment to 1
#pragma pack(push, 1)
struct SomeStruct
{
char someString[15];
int someInt;
};
// Reset alignment to default value
#pragma pack(pop)


If you are using GCC, you can do the following:

struct SomeStruct
{
char someString[15];
int someInt;
} __attribute__((aligned(1)));

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!