Public Group

# 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.

## Recommended Posts

I have a class:
class ByteBuffer
{
//other methods that work on the Buffer
private:
std::vector<char> Buffer;
};
also
class File
{
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 on other sites
If ImageFile is an object of type File, how are you able to call ImageFile.GetSize()? class File has no method GetSize().

##### Share on other sites
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 on other sites
Quote:
 Original post by jpetrieNote 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

1. 1
2. 2
3. 3
Rutin
20
4. 4
khawk
14
5. 5

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633655
• Total Posts
3013187
×