delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/04/01:34:45

From: Robert Hoehne <Robert DOT Hoehne AT Mathematik DOT TU-Chemnitz DOT DE>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: file finding function?
Date: Fri, 01 Aug 1997 12:20:41 +0200
Organization: TU Chemnitz-Zwickau
Lines: 42
Message-ID: <33E1B879.479A96C1@Mathematik.TU-Chemnitz.DE>
References: <01bc9d36$3e1e4fe0$7a3d31cf AT default>
NNTP-Posting-Host: prokyon.hrz.tu-chemnitz.de
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright 2019   by DJ Delorie     Updated Jul 2019