Sign in to follow this  
TigerKnee

C++ - reading in multiple data types from a txt file

Recommended Posts

I'm attempting to create a text-based RPG and my first task is to create code that will read in data from a txt file and then store it in the game variables itself so that I can easily just change the txt file itself if I want to change character stats etc.

After reading a couple of tutorials I came up with this code

[code]typedef struct
{
string name;
char symbol;
char gender;
char itemType;

int hp;
} PlayerData;


void loader(void)
{
// initialize
PlayerData player;

// read
string line;
ifstream fin("data.txt");
if (fin.is_open())
{
while (fin.good())
{
getline(fin,line);
stringstream ss(line);
string fieldname;

string svalue;
int ivalue;
char cvalue;

ss >> fieldname;
if (fieldname == "Name=")
{
ss >> svalue;
player.name = svalue;
cout << "name=" << player.name << "\n";
}
if (fieldname == "Symbol=")
{
ss >> cvalue;
player.symbol = cvalue;
cout << "symbol=" << player.symbol << "\n";
}
if (fieldname == "Gender=")
{
ss >> cvalue;
player.gender = cvalue;
cout << "gender=" << player.gender << "\n";
}
if (fieldname == "Item=")
{
ss >> cvalue;
player.itemType = cvalue;
cout << "Item=" << player.itemType << "\n";
}
if (fieldname == "HP=")
{
ss >> ivalue;
player.hp = ivalue;
cout << "HP=" << player.hp << "\n";
}
}
}
}[/code]

So far the code seems to work fine, but I have just one problem. I am unable to have certain values like the Name contain multiple words. For example, in my text file, "Name= Wolf" works as Wolf, "Name= DireWolf" works as DireWolf but "Name=Dire Wolf" just gives me Dire instead. Having no spaces for the names of every single item in the game will definitely look very ugly.

Is there a way to fix this problem?

Share this post


Link to post
Share on other sites
What happens if you change this

[code]
ss >> svalue;
player.name = svalue;
[/code]

with this

[code]
while(ss >> svalue)
player.name += svalue + " ";
[/code]

Basically what this does is to add all the remaining words from the line to the name.
FIXME: Adds an extra space at the end of the name.

If it works depends on the format of you text files though

Share this post


Link to post
Share on other sites
[quote name='pulpfist' timestamp='1318320349' post='4871372']
Basically what this does is to add all the remaining words from the line to the name.
FIXME: Adds an extra space at the end of the name.

If it works depends on the format of you text files though
[/quote]

Thanks, it does work (as far as I can tell for now, hopefully no other problems crop up later), although yes, there's an extra space at the end of the name. Not sure how to get rid of that (I'm extremely unfamiliar with strings)

For information, my txt file looks like this

"Name= Dire Wolf
Symbol= D
Gender= M
Item= H

HP= 70"

Share this post


Link to post
Share on other sites
Just write a simple strip function to remove leading and trailing spaces and tabs from a string.

Use find_first_not_of to find the first non-whitespace element, and find_last_not_of to find the last non-whitespace element. Then use substr to take the bit in between.

Share this post


Link to post
Share on other sites
Your names could be saved in the file as Dire_Wolf. Then simply remove any "_" in strings you find as you are parsing your text file, and insert a space. Do the opposite when saving to file.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this