How to sort files in some directory by the names on Linux

前端 未结 3 749
轻奢々
轻奢々 2020-12-17 16:25

I use opendir() and readdir() to display the file names in a directory. But they are disordered. How can I sort them? The language is C.

3条回答
  •  遥遥无期
    2020-12-17 16:53

    The idiomatic way to sort something in C is to use the qsort() function. For this to work, it's best if you can arrange to have all the file names collected into an array of pointers, and then you sort the array.

    This is not too hard, but it does require either a bit of dynamic-array management, or that you introduce static limits on things (maximum length of filenames, maximum number of files).

提交回复
热议问题