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

前端 未结 27 3154
情书的邮戳
情书的邮戳 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 <stdio.h>
    #include <sys/stat.h>
    #include <iostream>
    #include <dirent.h>
    #include <map>
    
    std::map<int, std::string> 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<int, std::string> 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<int, std::string> k = getFile("./", "");
        printMap(k);
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-21 06:13
    #include<iostream>
    #include <dirent.h>
    using namespace std;
    char ROOT[]={'.'};
    
    void listfiles(char* path){
        DIR * dirp = opendir(path);
        dirent * dp;
        while ( (dp = readdir(dirp)) !=NULL ) {
             cout << dp->d_name << " size " << dp->d_reclen<<std::endl;
        }
        (void)closedir(dirp);
    }
    
    int main(int argc, char **argv)
    {
        char* path;
        if (argc>1) path=argv[1]; else path=ROOT;
    
        cout<<"list files in ["<<path<<"]"<<std::endl;
        listfiles(path);
    
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-21 06:14

    One function is enough, you don't need to use any 3rd-party library (for Windows).

    #include <Windows.h>
    
    vector<string> get_all_files_names_within_folder(string folder)
    {
        vector<string> names;
        string search_path = folder + "/*.*";
        WIN32_FIND_DATA fd; 
        HANDLE hFind = ::FindFirstFile(search_path.c_str(), &fd); 
        if(hFind != INVALID_HANDLE_VALUE) { 
            do { 
                // read all (real) files in current folder
                // , delete '!' read other 2 default folder . and ..
                if(! (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ) {
                    names.push_back(fd.cFileName);
                }
            }while(::FindNextFile(hFind, &fd)); 
            ::FindClose(hFind); 
        } 
        return names;
    }
    

    PS: as mentioned by @Sebastian, you could change *.* to *.ext in order to get only the EXT-files (i.e. of a specific type) in that directory.

    0 讨论(0)
  • 2020-11-21 06:17

    Try boost for x-platform method

    http://www.boost.org/doc/libs/1_38_0/libs/filesystem/doc/index.htm

    or just use your OS specific file stuff.

    0 讨论(0)
  • 2020-11-21 06:18

    Building on what herohuyongtao posted and a few other posts:

    http://www.cplusplus.com/forum/general/39766/

    What is the expected input type of FindFirstFile?

    How to convert wstring into string?

    This is a Windows solution.

    Since I wanted to pass in std::string and return a vector of strings I had to make a couple conversions.

    #include <string>
    #include <Windows.h>
    #include <vector>
    #include <locale>
    #include <codecvt>
    
    std::vector<std::string> listFilesInDir(std::string path)
    {
        std::vector<std::string> names;
        //Convert string to wstring
        std::wstring search_path = std::wstring_convert<std::codecvt_utf8<wchar_t>>().from_bytes(path);
        WIN32_FIND_DATA fd;
        HANDLE hFind = FindFirstFile(search_path.c_str(), &fd);
        if (hFind != INVALID_HANDLE_VALUE) 
        {
            do 
            {
                // read all (real) files in current folder
                // , delete '!' read other 2 default folder . and ..
                if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) 
                {
                    //convert from wide char to narrow char array
                    char ch[260];
                    char DefChar = ' ';
                    WideCharToMultiByte(CP_ACP, 0, fd.cFileName, -1, ch, 260, &DefChar, NULL);
                    names.push_back(ch);
                }
            } 
            while (::FindNextFile(hFind, &fd));
            ::FindClose(hFind);
        }
        return names;
    }
    
    0 讨论(0)
  • 2020-11-21 06:20

    GNU Manual FTW

    http://www.gnu.org/software/libc/manual/html_node/Simple-Directory-Lister.html#Simple-Directory-Lister

    Also, sometimes it's good to go right to the source (pun intended). You can learn a lot by looking at the innards of some of the most common commands in Linux. I've set up a simple mirror of GNU's coreutils on github (for reading).

    https://github.com/homer6/gnu_coreutils/blob/master/src/ls.c

    Maybe this doesn't address Windows, but a number of cases of using Unix variants can be had by using these methods.

    Hope that helps...

    0 讨论(0)
提交回复
热议问题