您的位置:首页 > 运维架构 > Linux

Linux下获取目录下的文件列表,并按字符串排序

2014-02-22 15:43 260 查看
本程序大部分参考 man scandir 的内容:

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>

int main(void)
{
struct dirent **namelist;
int n;

n = scandir(".", &namelist, NULL, alphasort);
if (n < 0)
perror("scandir");
else {
while (n--) {
printf("%s\n", namelist
->d_name);
free(namelist
);
}
free(namelist);
}
}


此程序会获取当前目录下的所有文件名,并排序,然后把文件名按顺序打印出来。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息