Notifications
Clear all

To all SSC Station occupants

Thank you for the donations over the past year (2024), it is much appreciated. I am still trying to figure out how to migrate the forums to another community software (probably phpbb) but in the meantime I have updated the forum software to the latest version. SSC has been around a while so their is some very long time members here still using the site, thanks for making SSC home and sorry I haven't been as vocal as I should be in the forums I will try to improve my posting frequency.

Thank you again to all of the members that do take the time to donate a little, it helps keep this station functioning on the outer reaches of space.

-D1-

C++ File Question

(@derelict)
Active Member

Hey guys, I'm currently writing a C++ text-based game and I encountered a problem with writing save files.

Firstly, each bit of information is stored on a different line. I'm told I can't get to a specific line unless each variable is at a fixed length, which won't be. This is because some variables like the player's class will be at a fixed length, but others like the player's money will be constantly changing.

Simply put, I need to either sort out this way by reading a specific line in C++ or scrapping my current save file system and writing a new one; but I'll need to be able to read data that will not have a fixed length.

Hopefully the question made sense, if not say so and I'll try and rewrite it.

Thanks in advance, Alex.

Quote
Topic starter Posted : January 5, 2011 12:25
(@simbad)
Trusted Member

I have some code that reads in classic windows ini files with section and name=value lines

It needs the writing stuff.

But this should be no problem at all.

ReplyQuote
Posted : January 7, 2011 03:36
(@derelict)
Active Member

Well I fixed it by making the money a fixed length (always) like so:

The maximum number of credits would, for example, be 999,999,999. So in the file, I add one billion to the number, thus making it a consistent length all the time. For example, if you had 2500 credits in the file it would appear as 1,000,002,500. But when the file is parsed, a billion is deducted from the number; thus giving you the actual money number. And when the file is saved, it adds one billion to the number.

Just sharing this in case anyone encounters similar problems 🙂

ReplyQuote
Topic starter Posted : January 7, 2011 08:02
(@simbad)
Trusted Member

Tricky.

But I thought it would be easier to get the values with a method like this

amount=Config.Get("global.currency");

This would give you the value currency from section global.

And setting will work the same.

Config.Set("global.currency", "1000000");

Config.Save();

Only an idea.

ReplyQuote
Posted : January 7, 2011 09:10
(@derelict)
Active Member

I'm presuming by sections etc you mean like using .ini files? Well yes I always could, but I fixed it this way so I might as well keep it so. I've got a player structure which looks like this:

Code:
struct gPlayer // Player structure
{
int pRace;
int pClass;
int pFaction;
int pRank;
int pMoney;
int pHP;
int pMaxHP;
int pPower;
int pMaxPower;
int pZone;
};

gPlayer player;

Then basically to update the variables in player I do the following:

Code:
void pFile::Update()
{
ifstream saveFile ("player.dat");
int money;
int health;
int maxhealth;
saveFile.seekg(0, ios::beg) >> player.pRace;
saveFile.seekg(1, ios::beg) >> player.pClass;
saveFile.seekg(4, ios::beg) >> player.pFaction;
saveFile.seekg(7, ios::beg) >> player.pRank;
saveFile.seekg(10, ios::beg) >> money;
saveFile.seekg(23, ios::beg) >> health;
saveFile.seekg(32, ios::beg) >> maxhealth;
saveFile.seekg(38, ios::beg) >> player.pPower;
saveFile.seekg(43, ios::beg) >> player.pMaxPower;
saveFile.seekg(48, ios::beg) >> player.pZone;
player.pMoney = money - moneyAdd;
player.pHP = health - hpAdd;
player.pMaxHP = maxhealth - hpAdd;
saveFile.close();
}

And to save the data into the file I'd do this:

Code:
void pFile::Save()
{
ofstream saveFile ("player.dat");
saveFile << player.pRace << endl;
saveFile << player.pClass << endl;
saveFile << player.pFaction << endl;
saveFile << player.pRank << endl;
saveFile << player.pMoney + moneyAdd << endl;
saveFile << player.pHP + hpAdd << endl;
saveFile << player.pMaxHP + hpAdd << endl;
saveFile << player.pPower << endl;
saveFile << player.pMaxPower << endl;
saveFile << player.pZone << endl;
saveFile.close();
}

For reference, the save file I am using (player.dat) looks like the following:

Code:
1
3
1
6
1000002500
100100
100100
100
100
1
ReplyQuote
Topic starter Posted : January 7, 2011 11:58
(@simbad)
Trusted Member

You can store the data as hex ascii . Than you always have lines of same length.

ReplyQuote
Posted : January 7, 2011 21:55
(@derelict)
Active Member
Simbad wrote:
You can store the data as hex ascii . Than you always have lines of same length.

That's a good idea, and if I find myself constantly changing the save file structure I'll rewrite the code so it uses that method.

Thanks!

ReplyQuote
Topic starter Posted : January 8, 2011 01:02