Click here to Skip to main content
15,917,062 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Any Ideas to prevent duplicate entries in every execution of a program in a same text file.

Thanks in Advance :)

What I have tried:

Still searching for good ideas :(
Posted
Updated 3-Nov-22 14:44pm
v2
Comments
Richard MacCutchan 3-Nov-22 13:22pm    
Since we have no idea what your code is doing, or trying to do, it is difficult to give a reasonable answer. If you are adding to the file each time then you need to use some method to check that the data you add is not already present.
Abishek Samuel 3-Nov-22 13:27pm    
yea I am asking for any good method to check the data you add is not already present.
Richard MacCutchan 3-Nov-22 14:33pm    
Well you still have not given us any clues as to what your code is doing, what data your file contains, what data you are going to add to the file ... Please use the Improve question link above, and add the complete details.

You're going to have to read the text file in to know whats there.
If the file is "small" (compared to available RAM), then you might use a std::vector or a std::map to store the contents. If the file is "large", then you might need to scan the entire file on output. Or, if you could create a hash of the file lines, you could then check the hash value of the new output. But in the latter case, beware of collisions.
 
Share this answer
 
The only way to check is to read every entry in the text file, and check each and every one of them. Text files on their own don't have any organisation - not even lines - so you can't add "extra info" like a hash value to make the process quicker or easier unless you add it to a separate file and maintain that because separate app instances even on the same machine do not share memory.
 
Share this answer
 
I had to do something like this just today. What differed was I was comparing two directories and listing the files that one directory was missing but it was essentially exactly what you need to do. I made a list of all the files in the "base" directory and then for every file in the other directory I looked for it in the list and if it wasn't there I displayed it. In my case, that list was actually a vector of strings. I had about two hundred files so I didn't both with a map or binary search and it was plenty fast enough, even in debug mode. I would recommend a similar tactic for you. You can save a vector of your input data items and then search for each new item in it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900