Reading/Searching text files

I have a class called "Type". In class type I have string name, string weakness[], string resistance[], string immunity[]. I have a file with the format:
Name=type1
Weaknesses=weak1, weak2, etc
Resistances=resist1, resist2, etc
Immunities=immune1, etc

I'm not sure what is the easiest/most efficient way to read and store the data from this though. I can't read the file line by line because I'm not sure how many strings there are going to be. I'm also not sure how to ignore the "=" signs and the ","s.

Is there a way to read the strings between "Name=" and "Weaknesses=" and so forth, so I can store the values in the correct members. I'm sorry if this is unclear, I tried to explain it as best as I could.
string weakness[], string resistance[], string immunity[]
You need to use one of the standard containers rather than trying to deal with arrays directly. Plus [] isn't really what you think it means, it's an incomplete specification.

If you're not sure what container to use, we tend to use vector as the default. So vector<string> weakness, vector<string> resistance, vector<string> immunity is what you'll have.

Containers are cool. You can add and remove things at runtime and query their size.

You're going to spend a bit of time parsing the input, but the code for that will be associated with Type as in:
 
std::istream& operator>>(std::istream& stream, Type& t);


Once that is written, you can write code like:
1
2
3
4
5
6
vector<Type> things;
std::ifstream s("types.txt");

Type t;
while (s >> t)
    things.push_back(t);


In general, you'll need to read one line of the file at a time and process it. There's an example of reading tokens from a line here:
http://en.cppreference.com/w/cpp/io/basic_istream/getline
Topic archived. No new replies allowed.