Solved

Opening a wildcard of filetype

Posted on 2006-11-07
5
275 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 84

Assisted Solution

by:ozo
ozo earned 200 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 84

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 50 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 250 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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why this code doesn't work? 8 97
Inorder binary search tree 5 158
Why does module make constant click noise ? 9 125
How to align numbers in C using the %d 2 89
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

911 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now