: >Hi,
: > Please give me some idea how to write a C program to read multple
: >using wildcard character, e.g. dun*.dat. I have tried with
: >fopen("dun*.dat","r") but it did not work.
: > Your help would be appreciated:)
: >Regards,
: >Pisut T.
: To read multiple files you need to get the file names, and then open
: a file pointer for each file (or read them one at a time or whatever :)
: Read the man pages for readdir (or opendir, directory ...)
: From the man page on readdir() (section 3c on my Solaris box):
: ...
: EXAMPLES
: Here is a sample program that prints the names of all the
: files in the current directory:
:
: #include <stdio.h>
: #include <dirent.h>
:
: main()
: {
: DIR *dirp;
: struct dirent *direntp;
:
: dirp = opendir( "." );
: while ( (direntp = readdir( dirp )) != NULL )
: (void)printf( "%s\n", direntp->d_name );
: (void)closedir( dirp );
: return (0);
: }
:
: You should also check out the man page for dirent (section 4).
: I think this should be the same on other UNIX variants.
Wow... I didn't know C had commands like `readdir' and what have you - my
book on C doesn't mention them *anywhere*. Still, I did a `man readdir' and
there it was... however `man dirent' told me there was no entry for dirent,
as did man 4 dirent. (I'm using OSF3.2 on a DEC3000 box)
To my point. All the examples given deal with either a single named file,
or all the files in a dir. What isn't clear (to me at least) is whether this
method allows you to specify *some* of the files in the directory using a
wildcard as the original poster asked. I suspect it isn't, although I haven't
tried. I have a way to do it, although it is cheesy - but hey, I only just
learnt about readdir. Anyway, here's what I do...
ls -l dun*.dat | grep -v filelist.dat | awk '{print $9}' > /tmp/filelist.dat
This creates a file in /tmp with all the filenames you're interested in in
/tmp. Use a system() call to use this in a C prog. Then, fopen /tmp/filelist.dat,
and each line you read will contain one of the files you want to open.
fopen() each file in turn and do what you like...
like I say, its cheesy but it *does* work with wildcards... If theres a
better way, I could do with learning it myself.