opendir/readdir - Don't they access directories in chrono. order ?

1 view
Skip to first unread message

John F Haugh II

Nov 14, 1991, 9:06:09 AM11/14/91
[ Not really an internals question, so I'm sending this off to
comp.unix.questions. ]

In article <> (Jaideep Mirchandani) writes:
> I am trying to run a program which uses opendir/readdir
>to read the entries in a directory. I thought that this would
>access the entries in chronological order, so I set the timestamps of
>all the files in this directory in the order in which I wanted them read,
>by using 'touch'.
> Unfortunately, they were not read in time order, so I don't know how
>to control the program's output. Do you know how I can force opendir/readdir
>to read my file entries in a certain order.

readdir() returns the directory entries in some generally random
order, sort of related to time, but jumbled up as time goes by.
That is, if you start with an empty directory and add files to
it one by one, readdir() might give you the entries back in the
order you entered them. But generally speaking, it doesn't.

What you will probably have to do is set up a structure containing
the appropriate st_?time field and a pointer the file's name. Use
qsort() (or roll your own) and sort the resulting array of file
name/timestamp structures.
John F. Haugh II | I am the NRA. | UUCP: ...!!rpp386!jfh
Ma Bell: (512) 255-8251 | Shoot a friend today!| Domain:
" ... expectation is the mother of disappointment."
-- Brad Konopik


Aug 7, 2022, 7:58:14 PMAug 7
In your loolie.
Reply all
Reply to author
0 new messages