Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7679987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:00:20+00:00 2026-05-31T18:00:20+00:00

I’m having some trouble with the following function. It is supposed to be given

  • 0

I’m having some trouble with the following function. It is supposed to be given a path and a set of allowed file extensions, then find all files in that path with any of those extensions. Instead it finds nothing and returns an empty set.

std::set<boostfs::path> scan_directory(const boostfs::path& p,
                                       const bool recurse,
                                       const std::set<std::string>& allowed) {
    std::string ext ;
    std::set<boostfs::path> incs, incs2 ;
    boostfs::path::iterator itr ;

    // Extract directory and filename
    boostfs::path file = p.filename() ;
    boostfs::path dir = p.parent_path() ;

    std::cout << "path: " << p.string() << std::endl ;

    for (itr = dir.begin(); itr != dir.end(); ++itr) {
        if (boostfs::is_directory(*itr)) {
            if (recurse) {
                std::cout << "dir: " << itr->string() << std::endl ;
                incs2 = scan_directory(*itr, true, allowed) ;
                incs.insert(incs2.begin(), incs2.end()) ;
            }
        } else {
            // Don't include the original source
            if (*itr != p) {
                // Include only allowed file types
                ext = itr->extension().string() ;
                std::cout << "file: " << itr->string() << std::endl ;
                std::cout << "ext: " << ext << std::endl ;
                if (allowed.find(ext) != allowed.end()) {
                    incs.insert(*itr) ;
                }
            }
        }
    }

    return incs ;
}

The prints to cout are just for debugging. I’m testing it with the following directory structure:

./test/cpp/
    foo.cpp
    foo.h
    test.cpp
./test/cpp/bar/
    baz.cpp
    baz.h

I invoke the function with the path “test/cpp/test.cpp”, recurse true and a set containing one string “.cpp”. I get the following output from the prints,

path: test/cpp/test.cpp
dir: test
path: test
file: cpp
ext:

Then the function ends and the rest of the program continues, only it’s given an empty set of files so not much to work on. Given the test directory it should return a set containing “test/cpp/foo.cpp” and “test/cpp/bar/baz.cpp”.

I’m fairly sure it worked not long ago, but I’m not sure when it broke or what I did that made it do so. I’m sure it’s some small, annoying technicality.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:00:21+00:00Added an answer on May 31, 2026 at 6:00 pm

    I found my problem. I was using path::iterator instead of directory_iterator (or recursive_directory_iterator) so I was looping through the components of the path instead of the contents of the directory. I could have sworn it worked earlier, but that might just have been luck maybe.

    Here’s my working code

    std::set<boostfs::path> scan_directory(const boostfs::path& p,
                                           const bool recurse,
                                           const std::set<std::string>& allowed) {
        std::string ext ;
        std::set<boostfs::path> incs ;
    
        // Extract directory and filename
        boostfs::path file = p.filename() ;
        boostfs::path dir = p.parent_path() ;
    
        boostfs::recursive_directory_iterator itr(dir), itr_end ;
    
        while(itr != itr_end) {
            if (boostfs::is_directory(*itr)) {
                itr.no_push(!recurse) ;
            } else {
                // Don't include the original source
                if (*itr != p) {
                    // Include only allowed file types
                    ext = itr->path().extension().string() ;
                    if (allowed.find(ext) != allowed.end()) {
                        incs.insert(*itr) ;
                    }
                }
            }
    
            itr++ ;
        }
    
        return incs ;
    }
    

    I’ll let it be known that the examples of iterating through directories in Boost’s documentation are AWFUL

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
For some reason, after submitting a string like this Jack’s Spindle from a text
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.