如何通过Linux上的名字在某些目录中的文件进行排序 [英] How to sort files in some directory by the names on Linux

查看:134
本文介绍了如何通过Linux上的名字在某些目录中的文件进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用执行opendir() READDIR()来显示一个目录中的文件名。但他们是无序的。我该如何对它们进行排序?语言是C。

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.

推荐答案

排序用C东西的习惯的方法是使用的qsort()功能。对于这个工作,它是最好的,如果你可以安排都收集到一个指针数组的文件名,然后进行排序的数组。

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).

这篇关于如何通过Linux上的名字在某些目录中的文件进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆