Public Group

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

This topic is 2955 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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

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"; } } } }

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 on other sites
What happens if you change this

 ss >> svalue; player.name = svalue; 

with this

 while(ss >> svalue) player.name += svalue + " "; 

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 on other sites

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

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 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 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 on other sites
Alternative alternative: use getline, then find the first '=' on the line and then start reading from the first non-whitespace character after that.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 17
• 25
• 13
• 20