Solved

Opening a wildcard of filetype

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

760 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

21 Experts available now in Live!

Get 1:1 Help Now