0

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.stack.imgur.com/tQOZ6.png

Titanz
  • 3
  • 1
  • 5
  • you should clarify your question with examples of how the data is and how it suppose to be... – Samer Dec 24 '14 at 14:16
  • Instead of writing raw data of contact in each line why can't you write each contact as XML or JSON or some serialized manner in a single line? so that your loading part becomes much more easy... – Sridhar DD Dec 24 '14 at 14:17
  • @Samer I did my best to clarify the question, and actually WITH an example. – Titanz Dec 24 '14 at 14:26
  • @Sridhar DD man that is one valuable solution, I would definately try that one if that makes it easier, but atm I am unable to find tutorial on specifically xml files but I will try to figure it out. – Titanz Dec 24 '14 at 14:30
  • @Titanz:: Its really simple if you study and make it work and you dont need about trim in c++ and all.. – Sridhar DD Dec 24 '14 at 14:31
  • Yes very right, it seems the simplest one but there are many questions in my mind regarding the xml implementation, like, if i use simple txt file, I can use some tricks like for example, I want that if this thing comes in txt file"------------" a new node should get created. but I am not sure that using xml, it would be possible? – Titanz Dec 24 '14 at 14:49

2 Answers2

1

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/

Kuba
  • 839
  • 7
  • 16
  • I like this solution and also we need to trim the content to avoid the spaces and tabs(It seems like having spaces and tabs in image). – Sridhar DD Dec 24 '14 at 14:19
  • Unfortunately c++ standard string library is without nice trim. In java or php its default but here you have to write your own, or use boost library. http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring – Kuba Dec 24 '14 at 14:22
  • yes. We can write the trim functions like this http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring – Sridhar DD Dec 24 '14 at 14:30
  • This is the most sophisticated solution to the problem if implemented correctly as its implementation might be complicated. I am right onto it, trying to understand how your method can be implemented, I will report back. thanks, I could not upvote as I have below 15 rep. – Titanz Dec 24 '14 at 14:41
0

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.

Pzc
  • 88
  • 1
  • 3
  • 8
  • +100 for that, seems a very nice idea, in this way I can use the second variable (that is usable) and leave the rest. Thanks, will try and report back, but a downside is that this makes the code a bit not so sophisticated I would say. Thanks a ton. – Titanz Dec 24 '14 at 14:35