Reading From Files

Hey, let's say I have a file like this:

Example.txt
-----------------------------

value1 1 word
value2 2 word
value3 3 word
-----------------------------

How would I get it to read just the first two words of the 3 lines with "value" in them?

Like to store in an array where a specific value is related to the number beside it.

So how could I also change the number from a string to an int so I could print out a list like:

value1 = 1
value2 = 2
etc...

I want to use the integers for something, but I want the "value1" words to remain strings.

How would I go about something like that?

It always reads the entire line with getline(), and every single word as it loops when I use file >> temp. I just want to have the first two saved and the number to be an int for use later.
1
2
3
4
5
6
7
8
9
10
11
fstream file("filename.txt");
int i;
string str;
while(true){
   file >> str >> i;//read value1 to a string and 1 to an int.
   file.ignore(numeric_limits<streamsize>::max(), '\n');//ignore the rest of the line.
   if(!file) break;//if there was an error (end of file, for example) quit reading.
   //do something with str and i.
   //I guess what you need is to put them in a map.
}
file.close();
Topic archived. No new replies allowed.