Jump to content
  • Advertisement
Sign in to follow this  
VanillaSnake21

Read error while writing file?

This topic is 3622 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 have a class:
class ByteBuffer
{
//other methods that work on the Buffer
private:
std::vector<char> Buffer;
};
also
class File
{
FILEHEADER Header;
ByteBuffer Data;
};
The Data is a vector as you can see that is filled with data that was loaded from a different file. Header is valid also, and contains nothing but the type of file. Now I'm trying to writ this to a file like this:
 
        //File ImageFile; defined and intialized elesewhere
	ofstream OutFile(filename.c_str(), ios::binary);
	OutFile.write((char*)&ImageFile, ImageFile.GetSize());
	OutFile.close();
I get an access violation when I run the app. I debugged and I know that the ImageFile is getting initialized properly and the Header as well as the Data are all correct. Also the Data contains another file (bitmap) that I read in earlier, so the Data has to be valid. I also tried different sizes for write() it writes just fine up to about 15kb then somewhere at 19kb it gives me the error. Can someone please tell me what it's trying to read and how I figure out what the problem is? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
When you call write(), you cast the address of ImageFile object to a char* and write ImageFile.GetSize() bytes from that address. However, there is only valid memory from &ImageFile to &ImageFile + sizeof(ImageFile); ImageFile.GetSize() is probably returning something like the length of the vector stored inside ImageFile, or some value from a header or cached field someplace. That size is not likely sizeof(ImageFile).

The "solution" is to use sizeof(ImageFile) instead of ImageFile.GetSize(). However this will just cause the code not to crash, it still won't do what you want.

Writing a class or struct "directly" in the manner you're doing in the call to write() is unsafe in general. If you have appropriately controlled and guaranteed the padding within the class, you can get away with it if the class is of POD type -- but your class is not.

As such the real solution is that you must individually write() each element of the ImageFile object, as well as the data in the vector.

Note that in fact, since vector does dynamic allocation, the actual memory containing your data is probably nowhere near your ImageFile object. It's off on the heap somewhere. The method you're trying to use (rather than the correct one I just explained) will just write bullshit to the data file. When you try to read it back in, you won't have a sane object and you'll just crash when you try to use it.

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
Note that in fact, since vector does dynamic allocation, the actual memory containing your data is probably nowhere near your ImageFile object. It's off on the heap somewhere. The method you're trying to use (rather than the correct one I just explained) will just write bullshit to the data file. When you try to read it back in, you won't have a sane object and you'll just crash when you try to use it.


Oohh, right, I totally forgot that the data is not right there in my object. Thanks, jpetrie :D

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!