Recursive function call issue  
Author Message
asadim





PostPosted: Visual C++ General, Recursive function call issue Top

I have the following incomplete recursive function:

String* all_files(String* sDir)[]
{
String* f[] = Directory::GetFiles(sDir);
int numFiles = f->get_Length();

String* d[] = Directory::GetDirectories(sDir);
int numDirs = d->get_Length();

for (int j=0; j < numDirs; j++)
{
all_files(d[j]);
}

return files;
}

The above is supposed to return the full list of files in sDir. What it does successfully is going through every single file in sDir (including the files in sub-directories). What it doesn't is returning what it is supposed to return: an array containing the full path of all the files in sDir. The variable "files" is undefined because I wasn't sure where would be the best place to define it. Btw the above is written in C++, but using mscorlib.dll which makes it pretty much a C# code.

So my question is, what can I add to my function so that it achieves what it is supposed to achieve The problem is mainly due to the recursiveness of the function.

Any help is appreciated.


Visual C++12  
 
 
Mike Danes





PostPosted: Visual C++ General, Recursive function call issue Top

Since it is a recursive function you cannot return the list of files, you need to pass a list as a parameter and that list will be filled in:

void all_files(String* sDir, ArrayList *files)

{

files->AddRange(Directory::GetFiles(sDir));

String* d[] = Directory::GetDirectories(sDir);

int numDirs = d->get_Length();

for (int j = 0; j < numDirs; j++)

{

all_files(d[j], files);

}

}


 
 
asadim





PostPosted: Visual C++ General, Recursive function call issue Top

Thanks for your help. However I managed to solve it (the func name is changed to return_all_files):

/* Returns an array of strings containing all the files and their hash values
in the passed folder. The passed value to count should be 0.
Format of the array: {File1, Hash1, File2, Hash2, ...} */
String* return_all_files(String* sDir, int &count, String* files[] )[]
{
String* f[] = Directory::GetFiles(sDir);
int numFiles = f->get_Length();

for (int k=0; k < numFiles; k++){
files[count] = f[k];
files[count+1] = MD5hash(f[k]);
count+=2;
}

// Find the subfolders in the folder that is passed in.
String* d[] = Directory::GetDirectories(sDir);
int numDirs = d->get_Length();

for (int j=0; j < numDirs; j++)
{
return_all_files(d[j], count, files);
}

delete [] f;
delete [] d;
return files;
}

 
 
Mike Danes





PostPosted: Visual C++ General, Recursive function call issue Top

OK, but I'm curious what are you passing in for the files parameter, more specifically what size is the array that you pass in !!
 
 
asadim





PostPosted: Visual C++ General, Recursive function call issue Top

I use the following to define the array of strings to be passed for files[]:

String* temp [] = new String __gc* [(num_files*2)];

The variable num_files is calculated using some other algorithm that determines what the size of files[] is supposed to be when total_num_files() is called. So it is a pre-calculated value.