[Save Data Help] -



Save Data Help

Discuss Save Data Help



Posted by: gemiller

I am currently programming a text based game, and I want to save stats like health, gold, etc to a data file, which I know how to do, but, how do I make sure that the health always saves to line one and the gold to line two etc, because if it's not saved that way the input will end up reading the wrong thing, and say if they have 1 gold and 50 health, if it saves in a line, it may read 1 health, 50 gold, etc.



Posted by: kog

you'll need to use seekg and possibly tellg to position the file pointers



Posted by: gemiller

..well then, exactly how do I do that, can you give me some code examples..



Posted by: kog

Ok rather than use seekg you could use a struct to store the game info as followes

#include<<fstream.h>>
#include<<stdio.h>>

struct Info
{
int a,b,c;
char name[20];
};


void main()
{

Info info = {10,20,30,"jones"};

ofstream tfile( "c:\\myfile.txt" , ios::binary );
tfile.write( (char *) &info, sizeof info );
tfile.close();

ifstream tIn("C:\\myfile.txt");
tIn.read((char*)&info,sizeof info);
cout<<info.a<<" "<<info.b<<" "<<info.name<<endl;
tIn.close();
}




Posted by: MM

[QUOTE][i]Originally posted by kog [/i]
[B]
.
.

void main()
{
.
.
.
[/B][/QUOTE]

Please note that using "[b]void main()[/b]" is not legal in standard C or C++.

Read [url]http://www.eskimo.com/~scs/C-faq/q11.12.html[/url]
and
[url]http://www.eskimo.com/~scs/C-faq/q11.15.html[/url]



Posted by: gemiller

He meant int main is my guess.