Save bool variables in a file

Started by
4 comments, last by buumchakalaka 11 years, 4 months ago
Hi, Im stack with one code:
I have an array with 8 maps: ObjectMap map[8].
Each map have a member "bool unlock".
First, 7 maps are locked, when the player reach many objectives the maps become unlock.
I want to make a file like this (for save the state of the maps):

MapsState.txt
0 true
1 true
2 true
3 true
4 false
5 true
6 true
7 true

So, how can I write and read this file using fstreams?
I need map[0] get the bool state 0 from the file, map[1], bool state 1, etc.

Thanks for the help!
Advertisement
Output:

std::fstream file("MapsState.txt");
file << std::boolalpha; // This makes bools be output as "true"/"false" instead of "1"/"0" which is the default behavior
for (int i = 0; i < 8; ++i)
file << i << ' ' << map.unlock << '\n';


Input:

std::fstream file("MapsState.txt");
file >> boolalpha; // This tells the fstream to read bools as "true"/"false" instead of "1"/"0" which is the default behavior

int index;
bool unlock;
while ((file >> index) && (file >> unlock))
map[index].unlock = unlock;
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Thanks you! That is exactly what I need!
Hi again! Just one more cuestion related:
Can I write comments in a file? Like "//comment here" o /*comment*/, that the ">>" operator just ignore?
No. That'll require some extra parsing that you'll have to do yourself.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Thanks! That was all.

This topic is closed to new replies.

Advertisement