Jump to content
  • Advertisement
Sign in to follow this  
TigerKnee

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!