Insomnia strikes again, so I might as well spend the time trying to sleep to post something. This time I’m going to touch into a subject that I believe is the reason why I got the revelation about how memory management works. As the subject says, it’s about reading binary files.

The first thing you do is to include the #include <ifstream>

Next step is fairly simple:
std::ifstream filenamestream(“Filename with path and everything”, std::ios_base::binary);

Normally the filenamestream variable is called a stream. Often I wondered what they meant by that, but basically think of a stream of characters, yeah that’s right. Stream of characters. Makes sense? Not really, but it will. Because the smallest of the basic types that we programmers have available is the char type, it’s 1 byte big, perfect building block so to say. Now what happens under the hood is that to read the binary file, you need to read in enough data to make any sense of it. This you do by these simple lines:
int data = 0; // that’s a zero not an o
filenamestream.read((char*)&data, sizeof(int));

I know it doesn’t look very helpful, but here’s what happens, it looks at the sizeof parameter, you send in the reference to the type you want to insert the data into. It then assembles the 4 next binary characters into an int. Which basically means that the 4 * 1byte char turns into an int, which is very neat. Now this is of course just half the problem, because in order to read a file properly you need to know how a file is divided, but once you know it is very easy to assign the right data and make the file into data you can use.

However it doesn’t really stop there, let’s say you know you have several data types you want to read in at once, let’s say you know the structure is 4 ints, 3 floats, 3 ints and 1 char. You can basically do like this:
struct DataContainer{
int firstint, secondint, thirdint;
float firstfloat, secondfloat, thridfloat;
int fourthint, fifthint, sixthint;
char firstchar;
};

Then you can create a datacontainer:
Datacontainer mydatacontainer;
filestream.read((char*)&mydatacontainer, sizeof(Datacontainer);

It will then read the next 3 x int bytes (12chars), 3 x floats (12 bytes or 12 chars), 3 ints x 4 bytes again and lastly the 1byte character and assign it to the right spots in the memory, given that the sequence in the file is the same sequence. Just to give a real life example, our teacher created a mesh converter. We converted for instance a 3ds file into a .mesh, which contained data such as position, normal if that was available, texture coordinates if available and colours if available. Indices was automatically added, but that was also data that you could look for.

That meant that we had to write a program that read in those data and my first draft looked like this -> http://pastebin.com/mpQSDjgx
I could have used Vertex2 instead of individual ways of reading the characters in that example, however I knew that I had to change it to read different things from the file sooner or later, so I decided to take the individual datatypes instead of the whole chunk.

Anyway that’s what happens, reading one character at a time and just assemble it to the size and datatype that you want to insert it to. So basically when creating your own file format you do the same, you decide the structure to write the file to, then read it in the same order. I don’t know about networks, but it wouldn’t surprise me if the flow and the idea is the same, that you get a package of data that you “unwrap” the same way and you get the data you want and I believe depending on the protocol you use (UDP / TCP) you send feedback that you have received the data. If my understanding is correct UDP just “spams” out data and doesn’t need a confirmation of receiving the data before sending the next package, whereas the TCP protocol need to get a confirmation that the client received the data before sending a new package. That however is a topic for big game when we’re going to create a networked multiplayer game.

Anyway that’s beyond this topic, although I believe the idea is the same.