Thread: Filter directory search results.

  1. #1
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115

    Filter directory search results.

    Hi all, I have a function that returns the contents of a directory in a string vector. It works perfectly but what I want to do is filter out the results for a certain file type. I just wanted to know if there was anything already available to do this? Or would I just have to take the last 4 characters of the string and compare it? Below is the function I am using to get the contents of a directory.

    Code:
    int getdir (std::string dir, std::vector<std::string> &files)
    {
        DIR *dp;
        struct dirent *dirp;
        if((dp  = opendir(dir.c_str())) == NULL) {
            std::cout << "Error(" << errno << ") opening " << dir << std::endl;
            return errno;
        }
    
        while ((dirp = readdir(dp)) != NULL) {
            files.push_back(std::string(dirp->d_name));
        }
        closedir(dp);
        return 0;
    }

    EDIT:
    I have come up with this... but its not perfect… but it works…
    so again, is there a better way to do this?


    Code:
    int getdir (std::string dir, std::vector<std::string> &files, std::string filetype)
    {
        DIR *dp;
        struct dirent *dirp;
        if((dp  = opendir(dir.c_str())) == NULL) {
            std::cout << "Error(" << errno << ") opening " << dir << std::endl;
            return errno;
        }
    
        while ((dirp = readdir(dp)) != NULL) {
            if (std::string(dirp->d_name).find(filetype)!=std::string::npos)
               files.push_back(std::string(dirp->d_name));
        }
        closedir(dp);
        return 0;
    }
    Last edited by Loic; 08-17-2008 at 08:51 PM.

  2. #2
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    I would suggest substr so that you're comparing to the end instead of searching the whole string. If you''re looking for .txt files, foo.txt.bar would match with the current function

  3. #3
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    ok so i have come up with this...
    Code:
    int getdir (std::string dir, std::vector<std::string> &files, std::string filetype)
    {
        DIR *dp;
        struct dirent *dirp;
        if((dp  = opendir(dir.c_str())) == NULL) {
            std::cout << "Error(" << errno << ") opening " << dir << std::endl;
            return errno;
        }
    
        while ((dirp = readdir(dp)) != NULL) {
            if (std::string(dirp->d_name).substr((std::string(dirp->d_name).length()-filetype.length()),filetype.length()).find(filetype)!= std::string::npos )
               files.push_back(std::string(dirp->d_name));
        }
        closedir(dp);
        return 0;
    }

    it compiles fine, but when i try to run it i get this error...
    Code:
    This application has requested the Runtime to terminate it in an unusual way.
    Please contact the application's support team for more information.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Wearing the hat of guessing, I would suspect that
    Code:
    std::string(dirp->d_name).length()-filetype.length())
    becomes negative - for example, the filename "." and ".." to be shorter than some filetypes.

    As a side note:
    std::string(dirp->d_name) is repeated three times in this line:
    Code:
    if (std::string(dirp->d_name).substr((std::string(dirp->d_name).length()-filetype.length()),filetype.length()).find(filetype)!= std::string::npos )
               files.push_back(std::string(dirp->d_name));
    You should probably create a temporary variable.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Just a recommendation for the boost::filesystem library if you're going to do a lot of this kind of work.

  6. #6
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Quote Originally Posted by matsp View Post
    Wearing the hat of guessing, I would suspect that
    Code:
    std::string(dirp->d_name).length()-filetype.length())
    becomes negative - for example, the filename "." and ".." to be shorter than some filetypes.

    As a side note:
    std::string(dirp->d_name) is repeated three times in this line:
    Code:
    if (std::string(dirp->d_name).substr((std::string(dirp->d_name).length()-filetype.length()),filetype.length()).find(filetype)!= std::string::npos )
               files.push_back(std::string(dirp->d_name));
    You should probably create a temporary variable.

    --
    Mats
    Ah cheers for that. if I use that function with out the file type filtering the first two results are always “.” and “..”

    How could I get past this problem?

  7. #7
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Quote Originally Posted by rags_to_riches View Post
    Just a recommendation for the boost::filesystem library if you're going to do a lot of this kind of work.
    Cheers that looks good… ill have a read of that, but I still wouldn’t mind trying to get this other function I have to work.

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Quote Originally Posted by Loic View Post
    Ah cheers for that. if I use that function with out the file type filtering the first two results are always “.” and “..”

    How could I get past this problem?
    Use rfind?

  9. #9
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    Quote Originally Posted by Loic View Post
    Ah cheers for that. if I use that function with out the file type filtering the first two results are always “.” and “..”

    How could I get past this problem?
    Just put up a check comparing the string to . and .. before doing anything else

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Loic View Post
    Ah cheers for that. if I use that function with out the file type filtering the first two results are always “.” and “..”

    How could I get past this problem?
    Perhaps check if the length of the string is shorter than the length of the extension you are looking for? Then it can not possibly be a match, so no point in trying.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Kernel Sanders View Post
    Just put up a check comparing the string to . and .. before doing anything else
    Yes, but that doesn't prevent other short directory/filenames from causing the same problem. If you are searching for .txt, and a file is called a.c (my common name when I copy code from this site, a.c or x.c or some such - then I know it's nothing meaningfull or important), then it's shorter than extension, so it's still a problem.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Quote Originally Posted by matsp View Post
    Perhaps check if the length of the string is shorter than the length of the extension you are looking for? Then it can not possibly be a match, so no point in trying.

    --
    Mats
    Cheers, Thats worked

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Big help in Astar search code...
    By alvifarooq in forum C++ Programming
    Replies: 6
    Last Post: 09-24-2004, 11:38 AM
  4. linear search for structure (record) array
    By jereland in forum C Programming
    Replies: 3
    Last Post: 04-21-2004, 07:31 AM