Recommended Posts

I have created an almost fully functioning level creation program for a game I am making, the only problem is that once i start working on a level, I can't save or open the file. I figure I'm going to have to use iostream, but first I'm gonna need to know some things about what i should try and learn to accomplish this. The level creation program creates a map object, which creates a bunch of tiles that you can use to draw terrain on, originally when i started this project I thought I would just create i string separated by ","s for each tile and it would just have a number and that number would be what tile it is, but I'm thinking there must be a more convenient way of doing this then that. So I turned to my wondrous book of " c++ programming for the absolute beginner" and read something about binary streams, but it didn't go too much in detail about them, it just went on to string streams. So before I start scouring the internet for information, I'm wandering can I use a binary stream to store one of those Map object I mentioned in a file or can you only store things like integers boolean and chars, or what exactly can I store with it, would anyone be able to tell me how to use a binary stream or have any suggestions on a good place to learn about binary stream?

0

Share this post

Link to post

Share on other sites

Save yourself some pain; just use a text stream first, and learn the ins and outs of writing to and reading from files with iostream. Once you have a solid handle on that, you can consider if going binary would actually gain you anything or just be extra trouble for no real benefit.

0

Share this post

Link to post

Share on other sites

Binary streams read/write binary data. They have no notion of bool, int, float, or anything else. Yes, there may be some special functions designed to help you read/write certain data types, but for the most part you're on your own. If you create a custom class that you want to write out to a binary stream, you have to create a way for it to do that. Similarly, you have to create a way to read it in from a binary stream as well. One thing to be aware of in this process is endianness. Endianness basically has to do with how your computer stores data in its memory. An int may be 4 bytes, but in what order are those bytes stored? Most desktop machines today are little endian, but not all machines are. So if you want to write your data to a binary stream, you have to serialize it. Serialization is basically taking some data (whether it's an int, float, bool, or a custom class object) and arranging its data from whatever way the computer is storing it internally (because two computers may store it differently internally) to some kind of standardized data arrangement (which you have to come up with; basically it has to do with how you organize the bytes of that object so that two computers can agree on how bytes are organized when talking to each other, even if those bytes are organized differently on the internals of the machine while processing).

I've glossed over a lot of topics and points. It's ok if a lot of them are going over your head right now. Writing a robust program that can write/read a binary file on various different computers is pretty tricky (at first, once you get the hang of it it's not too bad). If you want two different computers to be able to share binary data, serialization is very important. If computers don't share data and only the computer that writes the file will the computer to read the file, a strict, careful serialization format is less important.

I would suggest starting small. Don't try to add this to your project all at once. Make a few smaller testing programs that read/write some binary files. Once you get the hang of writing and reading the files, add to the code to include some serialization/deserialization, particularly for custom class objects.

I like this tutorial on writing files, as it covers both text and binary files. Experiment with both. Understand both. Google functions or questions you might discover (or come back to GameDev.net). Sorry I don't have a step by step guide. But like ApochPiQ says, start with text files. They're so much easier to work with (and debug, because you can read with your eyes if you're writing something wrong). If you get the hang of it and discover a real need for binary files, then practice with binary files. Understanding writing/reading text files is like the foundation for writing/reading binary files.

String streams serialize the data for you. They just serialize the data to strings. That's an example of serialization.

0

Share this post

Link to post

Share on other sites

Alright, Thankyou so much for the advise, as well as that tutorial! I almost thought I may as well not even bother asking about this and just go straight into binary, I'm very glad I didn't. Also thanks for the information on what a binary stream actually is, and what is involved in making them useful, even if you did just "Gloss" over it, it's good to know more of what they actually are.