c++ - C++ 中的 Glob 并打印结果

标签 c++ glob stdio

我只是想遍历目录中的所有内容并打印结果列表,但我得到一个空的 printf:

#include <glob.h>
#include <stdio.h>

int main()
{
  int result;
  glob_t buffer;
  buffer.gl_offs = 10;
  glob("*", GLOB_DOOFFS, NULL, &buffer);
  printf((char*)buffer.gl_pathv);
}

有效的是

printf("%i", buffer.gl_pathc));

最佳答案

需要在glob中预留空位吗?如果不需要,请不要包含 GLOB_DOOFFS。 并且不要忘记为 glob 释放内存。

尝试这样的事情:

#include <glob.h>
#include <stdio.h>

int main() {

    glob_t globbuf;
    int err = glob("*", 0, NULL, &globbuf);
    if(err == 0)
    {
        for (size_t i = 0; i < globbuf.gl_pathc; i++)
        {
            printf("%s\n", globbuf.gl_pathv[i]);
        }

        globfree(&globbuf);
    }

    return 0;
}

关于c++ - C++ 中的 Glob 并打印结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15085633/

相关文章:

c - 二维char的fread fwrite问题

c++ - 是什么让 union 成员(member)活跃起来?

c++ - libpq : get data type

python - 在 Python 中减少路径名

bash - 在 bashrc 中为 GREP_OPTIONS 匹配文件类型 --exclude

c++ - 为什么 fscanf() 从不返回 EOF?

c++ - 将字符串解析为 float 时精度更高

c++ - 编译器何时在 C++ 中创建复制构造函数?

graphql - NextJs 使用 loadFilesSync 或 glob 加载多个文件,

c - 如何在 C 中刷新输入流?