Notifications
Clear all

C++ File Question


Derelict
(@derelict)
Crewman Registered
Joined: 13 years ago
Posts: 7
Topic starter  

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
Simbad
(@simbad)
Senior Chief Registered
Joined: 13 years ago
Posts: 61
 

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
Derelict
(@derelict)
Crewman Registered
Joined: 13 years ago
Posts: 7
Topic starter  

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
Simbad
(@simbad)
Senior Chief Registered
Joined: 13 years ago
Posts: 61
 

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
Derelict
(@derelict)
Crewman Registered
Joined: 13 years ago
Posts: 7
Topic starter  

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
Simbad
(@simbad)
Senior Chief Registered
Joined: 13 years ago
Posts: 61
 

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


ReplyQuote
Derelict
(@derelict)
Crewman Registered
Joined: 13 years ago
Posts: 7
Topic starter  
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