string line;
string word;
char delim = '\t';
getline(is, line);
istringstream iss(line);
getline(iss, word, delim);
detail.FirstName = word;
... etc.
Line 7 reads a entire line (up to the newline character '\n') into the string line.
Line 9 creates a stringstream called iss and loads the string line into it. A stringstream allows us to do input or output using a string, as if it was a file.
Then we use getline again, using the stringstream which we just created. This is the really useful part. We use the delimiter '\t' (or other value if you prefer) which allows the reading of an item which contains spaces.
word is all of the string up to the first delimiter, yes.
Then we repeat that getline, once for each item. Each time it reads the next portion of the string, starting just after the previous delimiter, and up to the next one.
also one more question what is causing it to read the next portion of the string . Why hasn't it gone back to the start.? I know its doing what I want it to do but ?
It's the stringstream, it mimics the behaviour of a normal file.
If you use getline() on an ordinary file, it reads the first line on the first call, and if you use getline again, it reads the second line and so on. It uses an internal pointer to keep track of the position it has reached in the file. Similarly the stringstream keeps track of the position within the stream.
Thanks but for some reason on test.txt file it is only keeping records of one person whoever was stored latest. I can't seem to pinpoint the reason for this
Here, the file is opened at line 2 of function DataEntry() and it will close at the end of the function. The file will be created fresh each time, thus only the most recent version will be kept.
There are several possible solutions. One is to open the file with a mode of ios::app, in order to append the new data at the end of the existing file.
Another possibility would be to open the file before calling the function DataEntry(), and close it after all the user-input has been done.
Yet another alternative would be to store all the user input in an array. Then have a function which opens the file, then loops through each array element and output from the array to the file.