Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Help saving multiple variables to a single file...

Author
Message
Crimson X
16
Years of Service
User Offline
Joined: 28th Nov 2007
Location: New Hampshire
Posted: 22nd Jan 2009 07:34 Edited at: 22nd Jan 2009 07:48
Alright so let's say I have two variables:

int id;
char name[20];

And I want to save them to a single file, then be able to call them back when the program loads again, what would I do? I can do the single variable fine:



As you can see first we check if the file exists, and if it doesn't we create it and write "100" to it. But what if I wanted to save a string in there as well, how would I sort out their positions when the file is read? Thanks in advance!
tneva82
16
Years of Service
User Offline
Joined: 7th May 2008
Location:
Posted: 22nd Jan 2009 12:10
Howabout good old C standard functions?



That would write line with int, float and string. Then you read it similary:



And this one reads same values. If you want multiple lines you need to add some sort of loop etc.
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 22nd Jan 2009 19:12
I'm with tneva82 with regards to using C/C++ standard functions. However, I prefer not to rely on saving things in text format, especially if it means revealing binary data to someone who might modify the information. True that for simple things it would be easy to find and modify binary data. But if the information is sufficiently complex it would deter the casual hacker.

Typically I'd do something like:



I'm a bit rusty on fstream so bear with me on this next part.


And, of course, reversing it for reading. The advantage is that you can write out whole lumps of data in one operation, including writing out an array of such structures that might define levels or identify objects.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Crimson X
16
Years of Service
User Offline
Joined: 28th Nov 2007
Location: New Hampshire
Posted: 23rd Jan 2009 00:23
Thank you guys, got it working nicely
tneva82
16
Years of Service
User Offline
Joined: 7th May 2008
Location:
Posted: 23rd Jan 2009 07:20
Quote: " However, I prefer not to rely on saving things in text format, especially if it means revealing binary data to someone who might modify the information. "


Fair enough but changing it to binary format shouldn't be too hard? Just add b to end of the mode

Anyway since I plan to leave data open for adjusting anyway text format suits me fine. I like it that you can adjust game by simply adjusting text files. Of course I'm not programming multiplayer games so that's not issue for me
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 23rd Jan 2009 11:51
Quote: "Fair enough but changing it to binary format shouldn't be too hard? Just add b to end of the mode"

That mode option simply suppresses the translation of carriage return and linefeed characters.

Login to post a reply

Server time is: 2024-09-30 15:29:21
Your offset time is: 2024-09-30 15:29:21