简体   繁体   English

Linux Dirent:获取目录中所有文件夹的列表

[英]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 . dirent结构实际上使用char*而不是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()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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