好的,我有这样的东西:
struct dirent *dp;
DIR *dir;
char fullname[MAXPATHLEN];
char** tmp_paths = argv[1]; //Not the exact code but you get the idea.
...
while ((dp = readdir(dir)) != NULL)
{
struct stat stat_buffer;
sprintf(fullname, "%s/%s", *tmp_paths, dp->d_name);
if (stat(fullname, &stat_buffer) != 0)
perror(opts.programname);
/* Processing files */
if (S_ISDIR(stat_buffer.st_mode))
{
nSubdirs++;
DIRECTORYINFO* subd = malloc(BUFSIZ);
}
/* Processing subdirs */
if (S_ISREG(stat_buffer.st_mode))
{
nFiles++;
FILEINFO *f = malloc(BUFSIZ);
}
}
如何将文件名和子目录名读入我自己的结构 DIRECTORYINFO 和 FILEINFO 中?我浏览了 stat.h,但没有发现任何有用的东西。
最佳答案
在 UNIX 世界中,名称不是文件的一部分,因此 stat(2)
无法检索有关它的信息。但在您的代码中,您的名称为 dp->d_name
,因此您可以将该字符串复制到您自己的数据结构中。这应该相当简单。
如果这不是你的问题,我没理解问题。
关于c - 如何从 C 中的目录中检索文件名和子目录名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4044969/