Save Data Help

Status
Not open for further replies.

gemiller

In Runtime
Messages
118
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.
 
you'll need to use seekg and possibly tellg to position the file pointers
 
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();
}
 
Status
Not open for further replies.
Back
Top Bottom