Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Opening a wildcard of filetype

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 opening and reading files in the C programming language.

860 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