Reputation: 13
I have managed to save all updated contacts to a .txt file. Now my next step is to load these contacts into linklist (as you know when the console is closed the linklist gets destroyed but contacts will still exist in .txt file, so what I have to do is: when I run the program next time, it should load the contacts saved from the txt file into linklists). Specifically I want to ask that is there any simple way that I can skip these "Name: " "Surname: " "Address: " "Phone Number: " parts for getting saved into the strings? PS I know how to load things from txt file but what I dont know is how to skip some of them. I will be using getline() for loading. I know one basic way for doing it is that I should not output these irrelevant parts into the txt file, but is it possible without it because I also want the txt file to be well detailed rather than in raw form. https://i.sstatic.net/tQOZ6.png
Upvotes: 0
Views: 80
Reputation: 88
Since the text is in such an easy format to parse and it's the same you can either read entire line with getline() like you're doing now to a buffer and copy the sub-strings you want to the actual linked list. Another solution would be to use cin a couple of times to a temporary variable to gobble up the garbage and then read data to variables which is easy since cin skips whitespace.
There's probably more fancy solutions but this should work.
Upvotes: 0
Reputation: 839
After getline you have everything you can find index of ":" using method find() or indexOf() from class 'string'. Then do the substr with this index to the end. Here is example of both functions http://www.cplusplus.com/reference/string/string/substr/
Upvotes: 1