Hello, im a c++ programmer, but there is something that i tried to learn and i couldnt find things, how do you guys store binary data?, ogre does it in the mesh format, im talking about writing a 12345 as a single Int and not an array of chars.
Any help will be appreciated.
Reading/writing binary files?
-
- OGRE Expert User
- Posts: 1920
- Joined: Sun Feb 19, 2012 9:24 pm
- Location: Russia
- x 201
Re: Reading/writing binary files?
Why wouldn't you look into the C++ code that Ogre has for reading/writing binary files then?
-
- Kobold
- Posts: 26
- Joined: Thu Jan 26, 2012 8:36 pm
Re: Reading/writing binary files?
I am doing that, but i want some guide or tutorials to learn it the good way instead of reverse engineering. I tried to find some resources about that in the web, but all i get is text based files, and i want full binary. Im learning graphics programming and im making my own engine, so i want to make my own simple format, just to learn. I did a game engine already with OGRE with physics, sound, gui, weapon and vehicle system and more things, but i want to increase my knowledge about 3d graphics, i may help a bit in ogre dev when i learn good 3d rendering
-
- OGRE Expert User
- Posts: 1920
- Joined: Sun Feb 19, 2012 9:24 pm
- Location: Russia
- x 201
Re: Reading/writing binary files?
Here's my quick tutorial. Shield your eyes
Edit: I believe you should figure out the reverse process of reading that back.

Code: Select all
#include <fstream>
int main()
{
std::ofstream os( "myfile.bin", std::ios::out | std::ios::binary );
const int singleInt = 12345;
os.write( reinterpret_cast< const char* >( &singleInt ), sizeof( singleInt ) );
os.close();
return 0;
}
-
- Kobold
- Posts: 26
- Joined: Thu Jan 26, 2012 8:36 pm
Re: Reading/writing binary files?
I see a const char* there, isnt that a normal string of chars?, i havent tested, but that looks like its making a normal c-string of the number. Also, how to read the values back?
-
- Goblin
- Posts: 260
- Joined: Tue Oct 25, 2011 1:07 am
- x 36
Re: Reading/writing binary files?
no need to reinvent the wheel
http://www.boost.org/doc/libs/1_52_0/li ... index.html
i have been using it for years. stable, feature rich, flexible and fast.
http://www.boost.org/doc/libs/1_52_0/li ... index.html
i have been using it for years. stable, feature rich, flexible and fast.
-
- OGRE Expert User
- Posts: 1920
- Joined: Sun Feb 19, 2012 9:24 pm
- Location: Russia
- x 201
Re: Reading/writing binary files?
That's what you get by using the "learn through tutorials" method. I see that too often lately. My next advice is to get, read, and understand the C++ bible by Bjarne Stroustrup. Without that and some good practice saying "i'm a c++ programmer" doesn't workvblanco wrote:I see a const char* there, isnt that a normal string of chars?, i havent tested, but that looks like its making a normal c-string of the number. Also, how to read the values back?

-
- OGRE Moderator
- Posts: 7157
- Joined: Sun Jan 25, 2004 7:35 am
- Location: Brisbane, Australia
- x 535
Re: Reading/writing binary files?
os.write takes the address of a byte in memory (a char is a byte, unless you are on freaky hardware) and a byte count. It will read that many bytes from the address and write them to the file.
This could be a C style string or just a block of bytes, it depends on how you use it, not how it's stored.
In the case above we are getting the address of singleInt, casting that address to pretend it's really pointing to a char instead of an int, then writing out as many bytes as an int has.
Pretty much any simple type can be written to a file like that. Cast it's pointer and use sizeof to get the number of bytes. But be careful of classes or structs that contain pointers or complex objects (so no string, vector, map, list, etc). That will cause problems when loading it back (the pointers will point to the wrong memory). Also never write out a class with virtual methods, the hidden vtable pointer will get written out and loading that back will do very bad things.
This could be a C style string or just a block of bytes, it depends on how you use it, not how it's stored.
In the case above we are getting the address of singleInt, casting that address to pretend it's really pointing to a char instead of an int, then writing out as many bytes as an int has.
Pretty much any simple type can be written to a file like that. Cast it's pointer and use sizeof to get the number of bytes. But be careful of classes or structs that contain pointers or complex objects (so no string, vector, map, list, etc). That will cause problems when loading it back (the pointers will point to the wrong memory). Also never write out a class with virtual methods, the hidden vtable pointer will get written out and loading that back will do very bad things.
-
- Kobold
- Posts: 26
- Joined: Thu Jan 26, 2012 8:36 pm
Re: Reading/writing binary files?
Thank you kojack, thats what i needed to know, i was confused becouse everywhere seems to use that cons char* for normal strings, and i dont use them i allways use std::string, ive never seen using it to another value