Linux 上readdir 遍历文件夹按文件名排序

遍历文件夹发送数据,数据文件名按时间排序,使用readdir 遍历时,没有按照文件名排序。
2017-10-15-00-00-00
2017-10-15-01-00-00
2017-10-15-02-00-00

最常见是readdir函数:

DIR   *dir;
struct  dirent *ptr;
dir = opendir(path.c_str());
if(NULL == dir)
{
    cout << "opendir is NULL" << endl;
    return;
}
while( (ptr = readdir(dir))!=NULL)
{
    printf("d_ino:%ld  d_off:%ld d_name: %s\n", ptr->d_ino,ptr->d_off,ptr->d_name);   
}
closedir(dir);

读取/etc/rc.d/目录,输出为:
d_ino:1048669 d_off:856980470154123585 d_name: rc5.d
d_ino:1048663 d_off:884029283848728392 d_name: init.d
d_ino:1048670 d_off:999482920564082138 d_name: rc6.d
d_ino:1059943 d_off:1785792148764494467 d_name: rc
d_ino:1048667 d_off:1841410420704204566 d_name: rc3.d
d_ino:1048666 d_off:3298607144791816940 d_name: rc2.d
d_ino:1048664 d_off:4425734956780595784 d_name: rc0.d
d_ino:1059945 d_off:5016870134774381628 d_name: rc.sysinit
d_ino:1048668 d_off:5182623748018784402 d_name: rc4.d
d_ino:1048665 d_off:6942952852705976198 d_name: rc1.d
d_ino:1048658 d_off:8782171503806864933 d_name: .
d_ino:1059944 d_off:9057741989092678028 d_name: rc.local
d_ino:1048577 d_off:9223372036854775807 d_name: ..

未按照文件名排序。其中:

struct dirent *readdir(DIR *dirp);
struct dirent   
{   
  long d_ino; /* inode number 索引节点号 */  
     
    off_t d_off; /* offset to this dirent 在目录文件中的偏移 */  
    unsigned short d_reclen; /* length of this d_name 文件名长 */  
    unsigned char d_type; /* the type of d_name 文件类型 */  
    char d_name [NAME_MAX+1]; /* file name (null-terminated) 文件名,最长255字符 */  
}

readdir函数按照在磁盘的索引顺序,d_off来排序,如果需要按照文件名d_name,需要遍历后将文件名保存,再次排序。

查阅得知:scandir 可以实现按照规则排序

#include <dirent.h>
int scandir(const char *dirp, struct dirent ***namelist,
    int (*filter)(const struct dirent *),
    int (*compar)(const struct dirent **, const struct dirent **));
int alphasort(const void *a, const void *b);
int versionsort(const void *a, const void *b);

alphasort() and scandir() are specified in POSIX.1-2008, and are widely available. version-sort() is a GNU extension.
The functions scandir() and alphasort() are from 4.3BSD, and have been available under Linux since libc4.

使用scandir遍历文件夹:

struct dirent **namelist;
int n;
n = scandir(path.c_str(),&namelist,0,alphasort);
if(n < 0)
{ 
    cout << "scandir return "<< n  << endl;
}
else
{
    int index=0;
    while(index < n)
    {
        printf("d_ino:%ld  d_off:%ld d_name: %s\n", namelist[index]->d_ino,namelist[index]->d_off,namelist[index]->d_name);
        free(namelist[index]);
        index++;
    }
    free(namelist);
}

输出:
d_ino:1048658 d_off:8782171503806864933 d_name: .
d_ino:1048577 d_off:9223372036854775807 d_name: ..
d_ino:1048663 d_off:884029283848728392 d_name: init.d
d_ino:1059943 d_off:1785792148764494467 d_name: rc
d_ino:1059944 d_off:9057741989092678028 d_name: rc.local
d_ino:1059945 d_off:5016870134774381628 d_name: rc.sysinit
d_ino:1048664 d_off:4425734956780595784 d_name: rc0.d
d_ino:1048665 d_off:6942952852705976198 d_name: rc1.d
d_ino:1048666 d_off:3298607144791816940 d_name: rc2.d
d_ino:1048667 d_off:1841410420704204566 d_name: rc3.d
d_ino:1048668 d_off:5182623748018784402 d_name: rc4.d
d_ino:1048669 d_off:856980470154123585 d_name: rc5.d
d_ino:1048670 d_off:999482920564082138 d_name: rc6.d

scandir函数中可以直接调用排序函数,将遍历到的文件名按照顺序保存在队列中。记得在使用后释放资源。

scandir函数通过参数4 调用比较函数实现排序,可以自定义int (*compar)(const struct dirent , const struct dirent )) 已按照某种规则来实现排序。

git@code.csdn.net:snippets/2599886.git
https://code.csdn.net/snippets/2599886

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值