Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: How to get each file name from a Unix dir in filename (not I-node) order.

Re: How to get each file name from a Unix dir in filename (not I-node) order.

From: Jack Klein <jackklein_at_worldnet.att.net>
Date: 1998/09/18
Message-ID: <360ad911.366854202@netnews.worldnet.att.net>#1/1

On Fri, 18 Sep 1998 11:31:38 +1000, Alister Miller <Alister_miller_at_nospamdse.vic.gov.au> wrote:

> I have a program (Pro*C) that loops through each file in a directory,
> but I want it get each file in filename order.
>
> I find that the program below does it in i-node order (due to the way
> "readdir" works), which is usually but not always the same as filename
> order.
>
> Can anyone help me?
>
> -------------------------------------------------------
> main

If you make the line above main() the program might compile, assuming you include stdio.h and the non ANSI/ISO dirent.h.

If you make the line above int main() or int main(void) it will even compile under the 1999 update, assuming you include the same headers.

> {
> char *sDirName[100];
> DIR *dp;
> struct dirent *direntp;
> char *sFileName[257];
>
> strcpy(sDirName, "/u04/users/fe/CASES_load");
>
> if ((dp = opendir(sDirName)) == NULL)
> { exit(EXIT_FAILURE); }
>
> while ((direntp = readdir(dp)) != NULL)
> {
> /* Get file name */
> sprintf((char *) sFileName, "%s", direntp->d_name);
> }
>
> closedir(dp);
> }
>
> -------------------------------------------------------
>
> Alister Miller
>
> (E-mail preferred. Remove "nospam" for return e-mail)
>

<Jack>

The functions opendir() and readdir() aren't part of C, of course they are POSIX. They don't actually define the order in which the files are read. If there are any options to make readdir() return file names in an order you prefer, I don't know what they are, and you'd need to ask about that in news:comp.unix.programmer.

You could always read them into an array and sort them any way you wanted.

</Jack> Received on Fri Sep 18 1998 - 00:00:00 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US