Mail Archives: djgpp/1997/08/04/01:34:45
Majisun wrote:
>
> Is there a function that will find out if there is any file with a certain
> extension in the current directory, and then return the filename?
There is no such explicit function. But you can write your
own very easy. Either you use the findfirst() function, which
is only available on DOS and it is in my opinion the fastest
or if you want to be portable also for unix systems, you can
use the combination of opendir(), readdir() and fnmatch(),
Below a short sample program which should work also on
DJGPP (I tested it only here on linux):
------------------------------------------------------
#include <stdio.h>
#include <dirent.h>
#include <fnmatch.h>
int main()
{
struct DIR *dir;
struct dirent *ent;
dir = opendir(".");
while ((ent = readdir(dir)) != NULL)
{
if (fnmatch("*.c",ent->d_name,0) != FNM_NOMATCH)
{
printf("found: %s\n",ent->d_name);
}
}
closedir(dir);
return 0;
}
--------------------------------------------------------
Robert
--
*****************************************************************
* Robert Hoehne, Fakultaet fuer Mathematik, TU-Chemnitz-Zwickau *
* Post: Am Berg 3, D-09573 Dittmannsdorf *
* e-Mail: Robert DOT Hoehne AT Mathematik DOT TU-Chemnitz DOT DE *
* WWW: http://www.tu-chemnitz.de/~rho *
*****************************************************************
- Raw text -