C++ File Question

Having trouble with some code, modding tools, game assets, php, html, mysql? Ask your questions and see if someone can give you a hand.
Post Reply
Derelict
Private
Posts: 7
Joined: Mon Jan 03, 2011 3:45 pm

C++ File Question

Post by Derelict »

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.
Simbad
Private
Posts: 61
Joined: Tue Oct 26, 2010 2:16 am

RE: C++ File Question

Post by Simbad »

I have some code that reads in classic windows ini files with section and name=value linesIt needs the writing stuff.But this should be no problem at all.
Derelict
Private
Posts: 7
Joined: Mon Jan 03, 2011 3:45 pm

RE: C++ File Question

Post by Derelict »

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 :)
Simbad
Private
Posts: 61
Joined: Tue Oct 26, 2010 2:16 am

RE: C++ File Question

Post by Simbad »

Tricky.But I thought it would be easier to get the values with a method like thisamount=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.
Derelict
Private
Posts: 7
Joined: Mon Jan 03, 2011 3:45 pm

RE: C++ File Question

Post by Derelict »

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:
131610000025001001001001001001001
Simbad
Private
Posts: 61
Joined: Tue Oct 26, 2010 2:16 am

RE: C++ File Question

Post by Simbad »

You can store the data as hex ascii . Than you always have lines of same length.
Derelict
Private
Posts: 7
Joined: Mon Jan 03, 2011 3:45 pm

RE: C++ File Question

Post by Derelict »


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!
Post Reply

Return to “General Coding and Mod discussion”