Here is the tutorial from this website that offers the information you need:
http://www.cplusplus.com/doc/tutorial/files/
Look a little less than half-way down the page at a section called "Text Files". You'll notice in their example that they have a while loop that will read from their file one line at a time.
To do this, they are using a function called getline(). The way this function works is that it will read an entire line from the file stream (passed as the first argument: infile) and save that entire line as a string the string variable that is passed as the second argument.
So at this point, you have the entire first line of your input file saved in a string (name and scores alike). Your next job is to separate it all. So you need to parse the line by using string member functions which are available here in this site's documentation (
http://www.cplusplus.com/reference/string/string/). You can use the string::find() commands to figure out where to split the line using the string::substr() command.
For example:
Suppose the first line needs to be split into name and scores...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
// The line variable contains the first line data of the file.
string line = Smithson 7.0 8.0 9.4 3.0 9.0;
// The find command with a space passed as the argument
// will search for the first space inside of the string and return the position.
int space_position = line.find(' ');
// Now that we know where the space is located, we know
// where to split the string to isolate the name.
// The first argument to substr() is the starting position of the new sub string.
// We want ours to start at the beginning because that is where the name is in
// the line, so we pass 0. The second argument is the amount of characters to
// take after the starting character. Since we are starting at the beginning,
// the position of the space that we found is equal to the length of the name.
string name = line.substr(0, space_position)
|
So, that tells you the process that we read lines from files and how we split them up into different forms of data. So to finish that line you need to then change your primary string to not include the name, so you can search for the next space without repeating what you just did.
The way to do that is to do like so:
1 2 3
|
// If you only pass the first argument it will assume you want
// everything following that position.
line = line.substr(space_position + 1);
|
I hope this helps. :)
Cheers,
Tresky
EDIT: Added string member functions link.