CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2008
    Posts
    5

    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

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Traverse directory using boost library

    It might be helpful to tell us which exception...
    Did you try to run it in a debugger?
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    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.
    - Alon

  4. #4
    Join Date
    Feb 2008
    Posts
    5

    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);
    }
    }
    }

  5. #5
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Traverse directory using boost library

    Did you try to run it in a debugger?
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured