Jump to content

  • Log In with Google      Sign In   
  • Create Account

Save bool variables in a file


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 buumchakalaka   Members   -  Reputation: 268

Like
1Likes
Like

Posted 15 December 2012 - 01:10 PM

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!

Edited by buumchakalaka, 15 December 2012 - 01:16 PM.


Sponsor:

#2 Cornstalks   Crossbones+   -  Reputation: 6990

Like
2Likes
Like

Posted 15 December 2012 - 01:30 PM

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[i].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;

[ 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 ]

#3 buumchakalaka   Members   -  Reputation: 268

Like
0Likes
Like

Posted 15 December 2012 - 01:47 PM

Thanks you! That is exactly what I need!

#4 buumchakalaka   Members   -  Reputation: 268

Like
0Likes
Like

Posted 15 December 2012 - 02:32 PM

Hi again! Just one more cuestion related:
Can I write comments in a file? Like "//comment here" o /*comment*/, that the ">>" operator just ignore?

#5 Cornstalks   Crossbones+   -  Reputation: 6990

Like
1Likes
Like

Posted 15 December 2012 - 02:36 PM

No. That'll require some extra parsing that you'll have to do yourself.
[ 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 ]

#6 buumchakalaka   Members   -  Reputation: 268

Like
0Likes
Like

Posted 15 December 2012 - 02:42 PM

Thanks! That was all.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS