问题
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));
回答1:
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;
}
来源:https://stackoverflow.com/questions/15085633/glob-in-c-and-print-the-results