0

I have a directory name called dir. It contain following files in order

12.07.2013
13.07.2013
14.07.2013
15.07.2013
16.07.2013
17.07.2013

I wrote following C program to display all the files from the directory dir

code :

#include <stdio.h>
#include <string.h>
#include <dirent.h>
int main (int argc, char *argv[])
{
    DIR *directory;
    struct dirent *file;     
    directory = opendir (argv[1]);
    if (directory != NULL){
        while (file = readdir (directory))
              printf("FILE : %s \n",file->d_name);

        (void) closedir (directory);
        }
   else
        printf("Not able to open the directory\n");
   return 0;
}

Above code give the actual output as

FILE : 14.07.2013 
FILE : 13.07.2013 
FILE : 17.07.2013 
FILE : . 
FILE : 15.07.2013 
FILE : .. 
FILE : 12.07.2013 
FILE : 16.07.2013 

but i expected output in proper order like below

FILE : 12.07.2013 
FILE : 13.07.2013 
FILE : 14.07.2013 
FILE : 15.07.2013 
FILE : 16.07.2013 
FILE : 17.07.2013 

When i directly see the files in directory it arranges & diplaying the files in proper order.

Then why above C code not reading file in proper order, i mean randomly reading the file.

Working environment : Linux(ubuntu12.04), gcc compiler

Thanks

sujin
  • 2,813
  • 2
  • 21
  • 33

1 Answers1

3

It's not randomly reading the files, it's just reading the directory listing in the order it's stored in the directory file itself. When you "directly see the files in directory", I presume that means you're using ls, but ls is sorting the results before output. If you want matching output you need to do the same.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • .. and `readdir()` doesn't exclude file names starting with `.` so you see `.` and `..` like in `ls -a` – Ingo Leonhardt Jul 17 '13 at 18:15
  • Thanks for your response.@carl : you mentioned "it's stored in the directory file itself" what it mean. – sujin Jul 17 '13 at 18:17
  • 1
    @sujin, directories are files, too. They just happen to contain lists of references to other files instead of being an image or a text file or a database or something else. – Carl Norum Jul 17 '13 at 18:17
  • @CarlNorum : is there any command to list the files in order, which my code display. – sujin Jul 17 '13 at 18:19
  • You mean is there a way to sort them? Probably lots. If you want standard C, look into `qsort`. – Carl Norum Jul 17 '13 at 18:21
  • no. you mentioned "order it's stored in the directory file itself". so is there any standard command to see the order you mentioned. – sujin Jul 17 '13 at 18:23
  • 1
    Yes - `ls -1aU` should do it for your case. – Carl Norum Jul 17 '13 at 18:25
  • Shouldn't `.` and `..` always be the first 2 entries read with `readdir()`? How could other been moved before them? – ott-- Jul 17 '13 at 18:34
  • @ott--, filesystem implementation detail, I guess. – Carl Norum Jul 17 '13 at 18:35
  • @ott-- if the filesystem uses a directory format optimized for fast lookups (like ext[234] with `dir_index` enabled) the order of names in a directory has little or no relationship to the order of their creation, so `.` and `..` can be anywhere. –  Jul 17 '13 at 19:26