file reading txt

Can anyone please explain the following code line by line in file reading

		string line;
		for (int i= 0; i<player; i++)
			stringstream val;
			val << arr[i];
			getline(val, line, ';');
			gamers[i].num = atoi(line.c_str());
line 2: You are creating variable line of type std::string (why not inside loop? Looks like it belongs there)
line 3: A loop for all players
line 5: you are reading line from file into array of strings with index corresponding of current player.
line 6: You are creating a stringstream variable. It works like any other stream
line 7: You are outputting value of arr[i] (or a line we get from a file before, actually) so our stringstream contains that string now.\
line 9: we are reading characters from val to the first encountered ';'
line 10: we are assigning to the integer member variable of some struct placed in a gamers array value of integer contained in line now. (And wy didn't they used stoi(line)?)
if this is the reading file txt:-

Round 1
Num Name Age Coins Score Distance
45 John 21 25 879 14000m
50 Takeshi 19 32 903 13657m

Round 2
Num Name Age Coins Score Distance
65 Dimítrios Régas 20 35 900 13000m
45 John Steffensen 21 20 600 10987m

Any idea how to store it in a single struct array?
create a structure called Record, add to this member variables that match your column headers (num, name, age etc etc).
Create an array of these records

in your for loop:
- new up a record
- when you get each line from the file parse it and populate each of the member variables (looks likeit's space separated variables?).
- add this record to your array.

so when the for loop completes you'll have a populated array of Records.
So how can we store the round 1, round 2 those details??
Topic archived. No new replies allowed.