?
Solved

Opening a wildcard of filetype

Posted on 2006-11-07
5
Medium Priority
?
307 Views
Last Modified: 2010-04-15
Hi,

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");
}
0
Comment
Question by:T3rminal
5 Comments
 
LVL 85

Assisted Solution

by:ozo
ozo earned 600 total points
ID: 17894304
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
0
 
LVL 85

Expert Comment

by:ozo
ID: 17894314
or on some systems
     #include <glob.h>

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

     void
     globfree(glob_t *pglob);
0
 

Author Comment

by:T3rminal
ID: 17894789
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.
0
 
LVL 19

Assisted Solution

by:BrianGEFF719
BrianGEFF719 earned 150 total points
ID: 17895853
In Windows you can use the FindFistFile(),FindNextFile(), PathMatchSpec() APIs to do what you want.
0
 
LVL 1

Accepted Solution

by:
mkruisselbrink earned 750 total points
ID: 17897090
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")
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question