【发布时间】:2019-04-06 18:40:05
【问题描述】:
使用opendir() 打开一个目录并使用readdir() 读出它的条目后,我检查该条目是否是符号链接,如果是,我想使用目标的名称。
(假设我们有一个目录exampledir,其中有一个名为linkfile 的符号链接文件链接到目录/path/to/link/target)
这是一个简化的 sn-p:
#include <sys/types.h>
#include <dirent.h>
// ...
const char *path_to_dir = "./exampledir";
DIR *dir = opendir(path_to_dir);
dirent *entry = readdir(dir);
if (entry->d_type == DT_LNK) {
// find out the link target's name and store / use it,
// but how...?
}
【问题讨论】:
-
请注意
dirent的d_type字段是非标准的,并且并非在所有系统上都存在。 -
还要注意
opendir和readdir可以在出现错误时返回NULL。