[英]Determine the type of directory entry a link points to
I want to read subdirectories and links to subdirectories only. 我想只读取子目录和子目录的链接。 With the following code I read all subdirs and links. 通过以下代码,我阅读了所有子目录和链接。
struct dirent* de;
DIR* dir = opendir(c_str());
if (!dir) { /* error handling */ }
while (NULL != (de = readdir(dir))) {
if (de->d_type != DT_DIR && de->d_type != DT_LNK)
continue;
// Do something with subdirectory
}
But how do I examine whether the link points to a subdirectory too? 但是如何检查链接是否也指向子目录呢? I do not want to read the entire linked directory to do this. 我不想读取整个链接目录来执行此操作。
You can use function named stat
from <sys/stat.h>
: 您可以使用<sys/stat.h>
名为stat
函数:
struct dirent* de;
struct stat s;
DIR* dir = opendir(c_str());
if (!dir) { /* error handling */ }
while (NULL != (de = readdir(dir))) {
if (de->d_type != DT_DIR) {
char filename[256];
sprintf(filename, "%s/%s", c_str(), de->d_name);
if (stat(filename, &s) < 0)
continue;
if (!S_ISDIR(s.st_mode))
continue;
}
// Do something with subdirectory
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.