How can I get the list of files in a directory using C or C++?

前端 未结 27 3170
情书的邮戳
情书的邮戳 2020-11-21 05:30

How can I determine the list of files in a directory from inside my C or C++ code?

I\'m not allowed to execute the ls command and parse the results from

27条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-21 06:13

    Just something that I want to share and thank you for the reading material. Play around with the function for a bit to understand it. You may like it. e stood for extension, p is for path, and s is for path separator.

    If the path is passed without ending separator, a separator will be appended to the path. For the extension, if an empty string is inputted then the function will return any file that does not have an extension in its name. If a single star was inputted than all files in the directory will be returned. If e length is greater than 0 but is not a single * then a dot will be prepended to e if e had not contained a dot at the zero position.

    For a returning value. If a zero-length map is returned then nothing was found but the directory was open okay. If index 999 is available from the return value but the map size is only 1 then that meant there was a problem with opening the directory path.

    Note that for efficiency, this function can be split into 3 smaller functions. On top of that, you can create a caller function that will detect which function it is going to call based on the input. Why is that more efficient? Said if you are going to grab everything that is a file, doing that method the subfunction that built for grabbing all the files will just grab all that are files and does not need to evaluate any other unnecessary condition everytime it found a file.

    That would also apply to when you grab files that do not have an extension. A specific built function for that purpose would only evaluate for weather if the object found is a file and then whether or not if the name of the file has a dot in it.

    The saving may not be much if you only read directories with not so much files. But if you are reading a mass amount of directory or if the directory has couple hundred thousands of files, it could be a huge saving.

    #include 
    #include 
    #include 
    #include 
    #include 
    
    std::map getFile(std::string p, std::string e = "", unsigned char s = '/'){
        if ( p.size() > 0 ){
            if (p.back() != s) p += s;
        }
        if ( e.size() > 0 ){
            if ( e.at(0) != '.' && !(e.size() == 1 && e.at(0) == '*') ) e = "." + e;
        }
    
        DIR *dir;
        struct dirent *ent;
        struct stat sb;
        std::map r = {{999, "FAILED"}};
        std::string temp;
        int f = 0;
        bool fd;
    
        if ( (dir = opendir(p.c_str())) != NULL ){
            r.erase (999);
            while ((ent = readdir (dir)) != NULL){
                temp = ent->d_name;
                fd = temp.find(".") != std::string::npos? true : false;
                temp = p + temp;
    
                if (stat(temp.c_str(), &sb) == 0 && S_ISREG(sb.st_mode)){
                    if ( e.size() == 1 && e.at(0) == '*' ){
                        r[f] = temp;
                        f++;
                    } else {
                        if (e.size() == 0){
                            if ( fd == false ){
                                r[f] = temp;
                                f++;
                            }
                            continue;
                        }
    
                        if (e.size() > temp.size()) continue;
    
                        if ( temp.substr(temp.size() - e.size()) == e ){
                            r[f] = temp;
                            f++;
                        }
                    }
                }
            }
    
            closedir(dir);
            return r;
        } else {
            return r;
        }
    }
    
    void printMap(auto &m){
        for (const auto &p : m) {
            std::cout << "m[" << p.first << "] = " << p.second << std::endl;
        }
    }
    
    int main(){
        std::map k = getFile("./", "");
        printMap(k);
        return 0;
    }
    

提交回复
热议问题