Path: utzoo!attcan!uunet!samsung!usc!apple!bbn!bbn.com!fkittred From: fkittred@bbn.com (Fletcher Kittredge) Newsgroups: comp.sys.hp Subject: Re: DIRECTORY (3C) strangeness Keywords: readdir not in alphabetical order? Message-ID: <48719@bbn.COM> Date: 26 Nov 89 03:33:36 GMT References: <616@mmlai.UUCP> Sender: news@bbn.COM Reply-To: fkittred@spca.bbn.com (Fletcher Kittredge) Organization: Bolt Beranek and Newman Inc., Cambridge MA Lines: 41 In article <616@mmlai.UUCP> burzio@mmlai.UUCP (Tony Burzio) writes: >System: HP-UX 6.5 B 9000/370 > >While trying out the C DIRECTORY(3C) routines, I noticed >some very strange behavior. When you do a readdir, >the files are returned in what seems to be random >order. At first it appeared that they return in the >order that the files are created, but new files pop >up where ever. The DIR pointer is not closed until >NULL is returned from readdir. Has anybody else used >this code and been able to extract file names in >alphabetical order? Where should I look for more info >besides the HP-UX reference? On a related note, is it >possible to run a sort on a tmpfile() created file? I >really need the filenames in order... Thanks in advance. Tony; If you intend to do programming on a Unix system, I strongly recommend you buy "The Unix Programming Environment" (Kernighan and Pike, 1984), and "The Design of the Unix Operating System" (Bach). The basic answer to your question is that you are assuming that directories are stored in sorted order. They are not; there is no reason for them to be stored sorted. You can look at the order they are stored in by entering the command: % strings |more For a justification of this design decision, any good intro to algorithms would help; my favorites are "Data Structures and Algorithms" (Aho, Hopcroft and Ullman, 1983), and Sedgewick's "Algorithms" (1988?). These would also be good places to look for sort algorithms to get your file names in order! happy hunting, fletcher P.S. If I read the manual correctly, you won't be able to sort a file created by tmpfile(), since it disappears from the file system immediately after it created. Fletcher E. Kittredge fkittred@bbn.com