Reading, writing, and modifying a txt file

Hello. I haven't done any c/c++ in quite some time and I'm rusty so please bear with me. It's been equally fun and frustrating trying to relearn it.

I need some assistance with a snippet of code. Hopefully I can explain this clearly. What I would like it to do is:
1.) Read in a .txt file. Each entry is on it's own line and is a name (string), followed by a space (' '), followed by a number (int).
2.) Search by name if there is an entry by that name in the file;
a.)If name is in file, add +1 to associated number
b.)If name is not in file, add the name with a number (1)
c.)If name is in .txt but not being searched for, do not modify.

For example my .txt file is:
tim 15
jim 10
bill 5

If I give it three names to check, ex. craig, jim, bill, it would not find craig and add craig to the list with 1, find jim and add 1 to jim, find bill and add 1 to bill, tim was not searched for so he stays at 15, making the text file at the end of execution:
tim 15
craig 1
jim 11
bill 6

Similarly, running it again querying for craig, jim, and bill would produce:
tim 15
craig 2
jim 12
bill 7

I have the rest of the code working but the problem I have is I can't seem to figure out the go about cycling through each name given and either updates the entry or adds it. Here's the code I have so far:
(Loop to iterate through each name I want to search for. This I have working perfectly.)
      fstream file("c:\\text.txt");
      string user;
      int pts;
      guy = xxxx; //name of current person being searched for, iterated through by my loop

	while (file >> user >> pts) // cycling through entrys. this seems to work somewhat.
	   if (guy == user)    //entry with same name as searched for found, updating entry by adding 1 to their number	
	        //not sure how to code this
 	   if (!(guy == user))  //entry with name searched for not found, add new entry with name guy and a number value of 1.
		ofstream file("c:\\text.txt", ios::app);
		file << guy << ' ' << "1" << endl; 						

So basically everything inside of the "while (file >> user >> pts)" loop, including the loop, I am not sure how to do. Perhaps I'm going about it completely wrong? Any help or insight would be much appreciated!
Look up the use of getline() it will save you much headache when you have to convert value types, handling new lines, spaces etc.
It's like you read my mind. What I didn't include is my conversion from int to string and others. It works, but it's sloppy. Getline() is very nice and much cleaner I will most definitely use that.

I'm still having trouble with the logic to read/modify my file.
I think I have almost gotten this working with two .txt files. Would rather not do it this way as I feel like it's sloppy but I'm not sure how to do it otherwise.

edit: going to go ahead and call this one solved. Instead of two files, I went ahead and opened the file, stored it in a structure, and will just manipulate data there and then overwrite old file.
Last edited on
Topic archived. No new replies allowed.