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!
Save bool variables in a file
Output:
Input:
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;
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement