reading from a text and ...

Write your question here.
hi everyone
sorry if this is too basic
I have a text file like this:
Number of Nodes:6
Number of Arcs:9
Origin:1
Destination:6
StartNode EndNode Cost
1 2 2
1 3 4
2 3 1
2 4 4
2 5 2
3 5 3
4 6 2
5 4 3
5 6 2
and I want the program to read this and make the following variables:
NumNodes(an integere that gets 6 from the tex)
numarcs(...)
origin
dest
and 3 arrays from[], to[] and cost[] which respectively will have the first and second and third colomn in them
Can anyone help me? :(
You can use getline to read number of nodes/arcs/etc, as they are all delimited by a colon(:).
http://www.cplusplus.com/reference/istream/istream/getline/

Then something like this to read start/end/cost, where fileIn is an ifstream object.
 
for( int i{}; fileIn >> from[i] >> to[i] >> cost[i]; i++ );

The above is equivalent to this:
1
2
3
int i{};
while( fileIn >> from[i] >> to[i] >> cost[i] )
    i++;
Last edited on
Topic archived. No new replies allowed.