This sounds like a place to use a
std::set - cppreference.com[
^] You need to look through the whole file for matches anyway, so why not read the entire file at the beginning of you program, putting each line into a
std::set<string>
. Then as you find your matches, call
find()
. If it returns
end()
, then you know its not in the set, so you need add the filename to the file of already matched files. Unless you think there's a good chance your disk search algorithm will find the same file multiple times, you don't need to add the file to the set.
If you don't need to append the file name right away, that is, the file update can wait until you've finished the scan, and it is not important that the text file be in order that the matches were found, then all you need to do is every time you find a match, just
insert()
the filename into the set. When you're done your scan, rewind the text file and write out the entire set. This will give the added benefit that the text file will always be in sorted order.
If the text file is just working as a database, and doesn't ever need to be read by a human, then maybe consider using a key/value store.