Click here to Skip to main content
15,909,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a function that searches through directory and then writes the path of the files in a text file
now what i want to do is that i need to match if during search files that are retrieved already present in this text file if not present then append them in the end of that text file otherwise if it matches with the path of the file that re already present then ignore them and don't write it to the file
this is my function


Now i know i can use eof to get files from text file and then match them with the path I have retreived but i cannot think of any logic

What I have tried:

I thought of this but it only matches the first line of text file and after matching it doesnt write but after that getfil function currFile doesn't change so it doesn't work
string str;
					do {
						while (getline(read, str))
						{
							if (str != currFile)
							{	//if(()
								file <</*counter<<"-"<< */"\n"<<currFile << endl;
								counter++;

							}
							else if (str == currFile)
							{
								break;
							}
							break;

						}
					} while (!read.eof());
Posted
Updated 19-Feb-20 18:14pm
v3

Use a std::set Luke, use a std::set!

Use the set to store the path of already found files (the ones present in your txt file).
Then, during search, insert the found files in the same set.
Eventually store the set content into the original txt file (overwriting it).
 
Share this answer
 
Comments
Member 12899279 19-Feb-20 11:43am    
got it working with using vector now searching time is increased significantly is there any better approach that would minimize the time search?
CPallini 19-Feb-20 13:30pm    
Try to use the std::set, instead.
Member 12899279 19-Feb-20 23:14pm    
reason?
CPallini 20-Feb-20 3:46am    
Because you don't have to check for duplicates, that's automatically handled by the data structure itself.
Member 12899279 20-Feb-20 9:46am    
yes i read about it but if we consider speed then vector is considerably better
Lots of redundant code in there:
C++
int getfil(void *args) // why are you using void* and not char* ?
{
    char *tmpPath = (char *)args; // why not just use the input argument ?
    string sPath(tmpPath);
    WIN32_FIND_DATA FindFileData;
    string sTmpPath = sPath; // why are you creating a copy of sPath ?
    sTmpPath += "\\*.*";

C++
// a neater way ...
int getfil(char *sourcePath)
{
    WIN32_FIND_DATA FindFileData;
    string sTmpPath = sourcePath;
    sTmpPath += "\\*.*";
 
Share this answer
 
v2
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, justinsert() 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.
 
Share this answer
 
v2

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