gpt4 book ai didi

c - dirent.h 将所有文件读取为 DT_UNKOWN

转载 作者:行者123 更新时间:2023-11-30 17:16:27 34 4
gpt4 key购买 nike

我正在使用dirent.h递归读取目录中的文件。在我的Debian GNU/Linux 7 (wheezy)上它工作正常,但是在Ubuntu 12.04 LTS上服务器将所有文件读取为 DT_UNKOWN!

if ((dir = opendir (input_dir)) != NULL)
{
while ((ent = readdir (dir)) != NULL)
{
// cat dir path to file
char full_file_path[FILE_NAME_LENGTH];
strcpy (full_file_path, input_dir);
strcat (full_file_path, "/");
strcat (full_file_path, ent->d_name);
// if "." or "..", skip it
if (!strcmp (ent->d_name, ".") || !strcmp (ent->d_name, ".."));
// if a regular file, process it
else if (ent->d_type == DT_REG)
{
process_file (full_file_path, f_out, z, ws);
}
// if a directory, recurse through it
else if (ent->d_type == DT_DIR)
{
// add '/' to the end of the directory path
process_directory (full_file_path, f_out, z, ws);
}
else
{
printf ("%s is neither a regular file nor a directory!\n",
full_file_path);
}
}
}

最佳答案

根据手册页(man 3 readdir):(虽然这是来自 Fedora,但我想 Ubuntu 不会有太大不同)

Currently, only some filesystems (among them: Btrfs, ext2, ext3, and ext4) have full support for returning the file type in d_type. All applications must properly handle a return of DT_UNKNOWN.

不保证可以从d_type读取文件类型。您应该转而使用 stat 来获取所需的信息。

关于c - dirent.h 将所有文件读取为 DT_UNKOWN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29628202/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com