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.
I use opendir() and readdir() to display the file names in a directory. But
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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).