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 
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 )) 已按照某种规则来实现排序。

[email protected]:snippets/2599886.git
https://code.csdn.net/snippets/2599886

你可能感兴趣的:([c++学习])