I've been trying to use the glob function in C to get a set of filenames in a directory where I store data. However I keep getting an error message that claims "Invalid Argument". I have no idea what argument it is refering to. Here is a sample code that produces the error
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <glob.h>
int main(int argc, char *argv[]){
int j = 0, err = 0;
glob_t *files = NULL;
err = glob("*", GLOB_ERR | GLOB_MARK, NULL, files);
if(err){
printf("Error found: %s\n",strerror(errno));
exit(err);
}
for(j = 0; j < files->gl_pathc; ++j){
printf("%s\n",files->gl_pathv[j]);
}
return 0;
}
Looking foward for any suggestions
The way you are currently passing files
there is no way glob()
could actually populate it. Rather, what you want to do is:
glob_t files = { 0 };
err = glob("*", GLOB_ERR | GLOB_MARK, NULL, &files);
You should also call globfree(&files)
later to clean up.