1

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

What does work is

printf("%i", buffer.gl_pathc));
joedborg
  • 17,651
  • 32
  • 84
  • 118

1 Answers1

3

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.

Try something like this:

#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;
}
Grigorii Chudnov
  • 3,046
  • 1
  • 25
  • 23