Jump to content
  • Advertisement
Sign in to follow this  
EvilCloneVlad

Reading in from a file and setting variables

This topic is 3502 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 was wondering if anyone could point me in the right direction for reading in information from a file, and then using what is read in to set a few variables. here is the situation: in the file chardata.txt is:
vladmere
warrior
10
9
1
1
now i want to read this file and put the data from each line into its own variable. what is the proper method for reading in the whole line that contains "vladmere" and storing it in a string variable called name. i know i need to start with:
ifstream myfile;
myfile.open("chardata.txt");
if(myfile.is_open())
{
      /*not sure how to read in the lines*/
}
any help would be appreciated.

Share this post


Link to post
Share on other sites
Advertisement
What you're looking for is the std::getline function.

string name;
getline(myfile, name);

It can also take an optional 3rd parameter to specify a character to stop reading at. See here for more info.

Share this post


Link to post
Share on other sites
ok so i am using the getline() function to retrieve the data from my file chardata.txt

my chardata.txt file contains:

vladmere
warrior
10
9
1
1


and here is the code I have to retrieve that data, it works fine if i just have the first two getlines() however when i added the rest to get the int values dev-c++ gives me an error saying "no matching function for call to `getline(std::ifstream&, int&)'"


string cname;
string cclass;
int char_attack;
int char_defense;
int char_power;
int char_knowledge;

ifstream chardata;
chardata.open("chardata.txt");
if(chardata.is_open())
{
getline(chardata, cname);
getline(chardata, cclass);
getline(chardata, char_attack);
getline(chardata, char_defense);
getline(chardata, char_power);
getline(chardata, char_knowledge);
}


im not understanding why it is having an issue with the ints

EDIT: does getline only work for reading in strings? If so how would I accomplish extracting the numbers as ints?

Share this post


Link to post
Share on other sites
Simple. getline doesn't understand ints it only understands strings. But that's fine! It's better to read in the input as a string and then try to convert it. Way less messy, especially since cin, although convenient, is pretty crappy at validating input. If you had boost installed (i'm guessing not) then you could just use lexical_cast which can convert between types very easily and intuitively. However, you probably don't have boost. But no worries! The standard library class stringstream allows us to convert from std::string to all of the standard types. I don't have a lot of time to write or find a great example, but if you go here you will get some sample code and a more thorough explanation that should get you started.

In order of preference I think you should do this:
1)use the boost library for lexical_cast
2)implement your own version of lexical_cast by using templates to transform from one type to another. If you already know how to use templates then doing this shouldn't be terribly difficult.
3)wrap the conversion into functions like: int stringToInt(const std::string &input); or double stringToDouble(const std::string &input);
4)convert from string to your desired data type directly inside your code

Hope this helps

Share this post


Link to post
Share on other sites
getline() reads a line into a string. It doesn't understand the concept of integers.

There are two solutions. You can simply start using operator >> once you have read the text. This assumes that there is only one game character in a particular file, because otherwise you will be mixing getline() and operator>>(), which can often lead to confusion. This is because operator>>() will stop at a newline, but also leave it in the buffer. A subsequent call to getline() will read and return an empty string. In contrast, getline() consumes the newline.

You can alternatively read the lines as strings and reparse them as integers. We can write a function to handle this for us:

#include <sstream>

std::istream &getline_integer(std::istream &in, int &value)
{
std::string string;
if(std::getline(in, string))
{
std::stringstream stream(string);
// Check if read successful
// Also check there is no more information in the stream
if(!(stream >> value && stream.eof()))
{
in.setstate(ios_base::failbit);
}
}

return in;
}

// meanwhile, back in the city...

string cname;
string cclass;
int char_attack;
int char_defense;
int char_power;
int char_knowledge;

// no need to manually call "open()", just use the constructor.
ifstream chardata("chardata.txt");

if(chardata.is_open())
{
getline(chardata, cname);
getline(chardata, cclass);
getline_integer(chardata, char_attack);
getline_integer(chardata, char_defense);
getline_integer(chardata, char_power);
getline_integer(chardata, char_knowledge);
}



A more robust version would use boost::lexical_cast<> instead of stringstream.

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!