Glob在C ++和打印结果 [英] Glob in C++ and print the results

查看:476
本文介绍了Glob在C ++和打印结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想在一个目录中的glob一切,并打印结果的列表,但我得到一个空printf:

I'm just trying to glob everything in a directory and print the list of results, but I get an empty 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_DOOFFS
不要忘记为glob存储空间。

Do you need to reserve empty slots in glob? Do not include GLOB_DOOFFS if you don't need it. And don't forget to free memory for 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;
}

这篇关于Glob在C ++和打印结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆