Traverse directory using boost library
I am using boost filesystem library to traverse directory. The function I wrote is to search a file in a directory and all its subdirectories.
I am using the following code to traverse the directory.
directory_iterator iter(dir_path), end_iter;
for(; iter!= end_iter; ++iter)
For most of the directories, this works beautifully. But when I try to search c:\
The program terminates with exception.
Anybody knowing what is the problem and how to avoid that?
Thanks
Re: Traverse directory using boost library
It might be helpful to tell us which exception...
Did you try to run it in a debugger?
Re: Traverse directory using boost library
Does C:\ appear in a string literal? If so, have you properly escaped the backslash (like "C:\\")?
A small, compilable example which produces the error would be helpful.
Re: Traverse directory using boost library
I don't really know what the exception is. It just says the program terminates.
The program works fine for most of the cases. Seems that when it meets some particular file in the c drive which cause the program to terminate. Very strange.
Here is the function
void find_file(const path & dir_path, const string & file_name, vector<path> & pfound)
{
if( !exists(dir_path) || !is_directory(dir_path) )
return ;
directory_iterator iter(dir_path), end_iter;
for(; iter!= end_iter; ++iter)
{
if( is_directory(*iter) )
{
find_file(*iter, file_name, pfound);
}
else if( wildcmp( file_name,iter->leaf())==1)//compare the file name to //see if they are the same. This supports wildcards
{
pfound.push_back( *iter);
}
}
}
Re: Traverse directory using boost library
Did you try to run it in a debugger?