• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 310
  • Last Modified:

Opening a wildcard of filetype


So you would have the below to open a single file e.g. 'hello.dat' but how would you go about opening '*.dat'? I assume you'd need some sort of way to check if any other files existed before opening the next.

int main(int argc, char * argv)
   File * mpp = fopen("argv[1]", "r");
3 Solutions
There is no way in pure C to check wildcards in a directory, but depending on your operating system, you might use
#include <sys/types.h>
     #include <dirent.h>

     DIR *
     opendir(const char *filename);

     struct dirent *
     readdir(DIR *dirp);
to look for matching file names
or on some systems
     #include <glob.h>

     glob(const char *pattern, int flags, int (*errfunc)(const char *, int),
         glob_t *pglob);

     globfree(glob_t *pglob);
T3rminalAuthor Commented:
Ya, I can see how the first would work, and could probably get that to file type eventually?  Not quite what I had in mind though, would be a lot of work to take a wildcard from command line.
In Windows you can use the FindFistFile(),FindNextFile(), PathMatchSpec() APIs to do what you want.
in most unix shells you wouldn't need to do anything to handle wildcards from the commandline, as the shell will expand *.dat to all matching files alread (so if files 1.dat and 2.dat exist, your program will be called with two arguments when calling "./foobar *.dat")
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now