Sign in to follow this  
buumchakalaka

Save bool variables in a file

Recommended Posts

buumchakalaka    268
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):

[u]MapsState.txt[/u]
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! Edited by buumchakalaka

Share this post


Link to post
Share on other sites
Cornstalks    7030
Output:
[code]
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[i].unlock << '\n';
[/code]

Input:
[code]
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;
[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this