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.
相关问题
- Multiple sockets for clients to connect to
- How to toggle on Order in ReactJS
- PHP Recursively File Folder Scan Sorted by Modific
- Is shmid returned by shmget() unique across proces
- What is the best way to do a search in a large fil
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).
Maybe you could use scandir() instead of opendir and readdir?
You have to dynamically construct a data structure containing the filenames and make sure it is sorted.
You could build an array or linked list with the names, and then sort that, but my preference is to sort the values on insertion by inserting into a binary tree.