Archived

This topic is now archived and is closed to further replies.

MagTDK

Recursion

Recommended Posts

I created a search function that will find a file in a directory of my choice. This includes searching through sub directory's as well. The problem is I felt I have painted myself into a corner when I created this function because it uses recursion. I say this because this is my first recursive function that I have coded and need some help on how after I find the file how do I stop the recursive process and return the file location? Here is my function that will find the file:
      
void Search::FindFile (std::string dir, std::string FileExt, std::string file)
{

	WIN32_FIND_DATA filedata;

	HANDLE DataFound;

	std::string TempName, thisdir;

	thisdir = dir + FileExt;

	DataFound = FindFirstFile (thisdir.c_str(), &filedata);

	if (DataFound != INVALID_HANDLE_VALUE) {
		do {
			// check if directory

			if (filedata.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) {

				TempName.clear();
				TempName.assign (filedata.cFileName);

				// exclude these directory's

				if (TempName != "." && TempName != "..") {

					TempName.clear();
					TempName = dir;
					TempName.append (filedata.cFileName);
					TempName.append ("\\");

					FindFile (TempName, FileExt, file);

				}

			// it's a file

			} else {

				TempName.clear();
				TempName.assign (filedata.cFileName);

				// check if this is the file

				if (TempName == file) {

					dir.append (TempName);

					// Now that I've found my file, I want to return the dir variable 

					// that contains the location to this thile. How do I do it?


				}
			}
		} while (FindNextFile(DataFound, &filedata));
	}

	FindClose(DataFound);

}
    
I call it like this: FindFile("c:\\somedir\\", "*.*", "mypic.bmp"); [edited by - MagTDK on July 17, 2002 8:25:56 PM]

Share this post


Link to post
Share on other sites
hi,

try returning a bool (or int) and return false (or whatever) to abort recursion:

1) modify the function to return a bool

2) modify the recursive call to return false if the recursion down returned false (this is the case when recursion should stop):
... Tempname.append ("\\");
if (FindFile (TempName, FileExt, file) == false)
return false;

3) return false if your file was found:
... if (TempName == file)
return false;

3) return true at the end of the function (recursion continues)

you may want to call FindClose() at 2) and 3) also.

regards,
fov

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It''s probably better to use a depth-first search for this purpose and literally ''break'' out of the loop when you find what you''re looking for. A recursive solution would have to pass the answer back up the call stack, or modify some global and still wait for all the frames on the call stack to be popped off. This only applies to C/C++ though. In Scheme you could pass a continuation to the function and also use proper tail-recursion.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Sorry, I meant a while-loop and explicit stack implementation of depth-first search.

Share this post


Link to post
Share on other sites
I was going to say, at some point in the recursive function there has to be a point where we actually decide to return before another recursion takes place, and whether or not we return a value depends on whether recursion depends on s sub-level recursion completing or not. Otherwise it will keep going. Forever ;P

Share this post


Link to post
Share on other sites