Does readdir() guarantee an order?

前端 未结 7 1471
时光取名叫无心
时光取名叫无心 2020-11-27 18:50

I\'m getting a list of files on a linux-like system using opendir/readdir. It appears that the directory entries are returned in alphabetical order of file name. However, I

7条回答
  •  时光取名叫无心
    2020-11-27 19:34

    In addition to the other answers, the readdir man page is pretty clear about files ordering.

    The order in which filenames are read by successive calls to readdir() depends on the filesystem implementation; it is unlikely that the names will be sorted in any fashion.

    Some file systems, like ReiserFS do list the files in lexical order.

    In your case, you have to store the names in an array, then sort the array.

    For instance, use qsort() to sort the array.

提交回复
热议问题