简体   繁体   中英

Linux Dirent: Getting a list of all folders inside a directory

Following is the code-snippet that I am using to get a list of all the folders within the current folder. I want to get red of the "." and ".." folders from the list but somehow cant.

const char* root_dir_c  = root_dir.c_str();
DIR *pdir               = opendir(root_dir_c);
struct dirent *entry    = readdir(pdir);

while (entry != NULL){
    if  ((entry->d_type == DT_DIR) && (entry->d_name != ".") && (entry->d_name != "..")){
        // DO STUFF
    }
    entry = readdir(pdir);
}

Can you please help ?

entry->d_name是一个char array ,不适用于!= ,您将需要使用strcmp或类似名称。

The dirent structure actually uses a char* not std::string . Thus you're going to compare two pointer values, which are never likely to be the same.

You have to use strcmp() for this case:

strcmp(entry->d_name,".") == 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM