Thursday, July 30, 2009

C++ input file question?

I have a given text file that I need to read from with the following conditions:


First line contains names of N places


Each successive line contains a name of one of the places followed by an integer value, then the names of the places it is connected to and an integer distance





Here is an example file:





Miami Orlando Detroit Dallas


Miami 10 Orlando 5 Dallas 20


Orlando 8 Dallas 15 Detroit 40


Detroit 15 Dallas 50


Dallas 20 Miami 10 Orlando 15





First line lists the places...second line lists Miami, it's value of 10, it's connections to Orlando and Dallas and its distances to them (distance to Orlando is 5, distance to Dallas 20).





Basically, I need to read a file like this in and store it in a vector...The vector is of type pair%26lt;Node%26lt;T%26gt;*, int%26gt; where T is just a templated type, int is the distance. So each location in the vector will contain a Node which will just be the name of the place, and the distance as a pair.





How would I read these files in?

C++ input file question?
I would suggest you use the getline() function to read each line into a string, then parse the string. You can use eof() to detect the eof, but with the rules you stated, that is necessary only to check for the file NOT following the rules.





Hope this helps.


No comments:

Post a Comment