I would highly recommend using a library to handle this for you while you get started - a good, lightweight example is Protocol Buffers, a serialization protocol designed by Google.
Protocol buffers are a flexible, efficient, automated mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages. You can even update your data structure without breaking deployed programs that are compiled against the "old" format.
If Google Protocol Buffers don't suite you, feel free to keep looking around, and I'm sure you'll find something you can use :)