• Advertisement
Sign in to follow this  

std::ifstream on 64-bit OS

This topic is 3611 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

I'm having some trouble running an MD2 Model Loader on Vista 64-bit. The loader itself works perfectly on 32-bit XP, so I figured all I would need to do is recompile the project in VS2008, sadly it doesn't seem to have had any effect. Here's the relevant section of code:
std::ifstream fFile;
S_MD2Header header;     

fFile.open(pFilename, std::ios::in | std::ios::binary);

if(!fFile.is_open())
{
	return false;
}
    
fFile.read(reinterpret_cast<char*>(&header), sizeof(S_MD2Header));

if((header.indent != MD2_IDENT) && (header.version != MD2_VERSION))
{
	fFile.close();
	return false;
}

The file is opened correctly but when I call std::ifstream::read() all that is placed inside the header is zeros. What am I missing?

Share this post


Link to post
Share on other sites
Advertisement
My first guess would be that the read call is failing in some way. You aren't checking for that at all. Process Monitor might be useful in tracking down the cause of any file reading problems.

It's also possible that sizeof(S_MD2Header) changes between the two versions, which would obviously mess things up.

Another thing to try would be to use fopen() and fread() instead of ifstream. They are sometimes easier to debug.

A minimal compilable example which shows the problem would be helpful, if those guesses aren't right.

Share this post


Link to post
Share on other sites
Quote:
Original post by FunLogic
What am I missing?

One thing you're missing is the fundmental fact that reading and writing a binary blob like that is inherently non-portable (across architectures, even across different build settinsg from the same compiler on the same architecture). You expectation of success in doing so is misplaced.

I suggest you google for information on object serialization. There are some widely available libraries that could help.

--smw

Share this post


Link to post
Share on other sites
You could have problems with the S_MD2Header structure, odds are that type sizes and/or alignments have changed. Having said that, that wouldn't cause the contents to be all 0's, you'd get garbage data. Try calling gcount() to see how many bytes were successfully read in the last operation.

Share this post


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

  • Advertisement