如何从C中的目录中检索文件名和子目录名?

发布于 2024-09-29 18:52:18 字数 749 浏览 0 评论 0原文

好的,我有这样的事情:

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 但没有发现任何有用的东西。

Ok I have something like this:

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);
    }
}

How do I go about reading in the file names and subdirectory names into my own structure DIRECTORYINFO and FILEINFO? I've gone through stat.h and haven't found anything useful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

能怎样 2024-10-06 18:52:18

在 UNIX 世界中,名称不是文件的一部分,因此 stat(2) 无法检索有关它的信息。但在您的代码中,您的名称为 dp->d_name,因此您可以将该字符串复制到您自己的数据结构中。这应该是相当简单的。

如果这不是你的问题,我不明白这个问题。

In the UNIX world the name is not part of the file, so stat(2) cannot retrieve information about it. But in your code you have the name as dp->d_name, so you can copy that string into your own data structure. That should be fairly simple.

If this is not your problem I didn't understand the question.

人海汹涌 2024-10-06 18:52:18

看看这个问题及其答案。您可能想使用dirent->d_name

Take a look at this question and its answers. You probably want to use dirent->d_name.

懒的傷心 2024-10-06 18:52:18

Glob 是你的朋友

glob() 函数根据 shell 使用的规则搜索所有匹配模式的路径名

/* Sample Code */
#include <glob.h>    
glob_t data;
glob("*", 0, NULL, &data ); /* Here "*" says to match all files of the current dir */
for(int i=0; i<data.gl_pathc; i++)
{
    /* Printing all the path names,Just for illustration */
    printf( "%s\n", data.gl_pathv[i] );
}
/* To split into DIRINFO and FILEINFO, stat(2) should be made use of */
globfree( &data ); /* free the data structure */

始终可以使用unix ma​​n 页面

人球

Glob is your friend

The glob() function searches for all the pathnames matching pattern according to the rules used by the shell

/* Sample Code */
#include <glob.h>    
glob_t data;
glob("*", 0, NULL, &data ); /* Here "*" says to match all files of the current dir */
for(int i=0; i<data.gl_pathc; i++)
{
    /* Printing all the path names,Just for illustration */
    printf( "%s\n", data.gl_pathv[i] );
}
/* To split into DIRINFO and FILEINFO, stat(2) should be made use of */
globfree( &data ); /* free the data structure */

To get more details you can always use the unix man page

man glob

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文