日期:2014-05-16  浏览次数:20748 次

关于readdir读出时的排序问题
linux 下 readdir 读出的文件或目录是按什么方式排序的啊?创建时间?还是?我想得到比较权威的观点,现在在做一个项目,感觉用这个函数非常好,现在苦于不知道readdir读出文件的排序方式。

------解决方案--------------------
struct dirent
{
long d_ino; /* inode number */
off_t d_off; /* offset to this dirent */
unsigned short d_reclen; /* length of this d_name */
char d_name [NAME_MAX+1]; /* filename (null-terminated) */
}

d_ino is an inode number. d_off is the distance from the start of the directory to this dirent. d_reclen is the size
of d_name, not counting the null terminator. d_name is a null-terminated filename.


是按照d_off 来排序的, 我写了个小程序作测试。
C/C++ code

#include<sys/types.h>
#include<stdio.h>
#include<dirent.h>  
#include<unistd.h>  
main()  
{  
  DIR   *dir;   
  struct   dirent   *ptr;   
  int   i;

  dir   =opendir(".");

  while((ptr   =   readdir(dir))!=NULL)
  {
         printf("  d_off:%d d_name: %s\n", ptr->d_off,ptr->d_name);   
  }
        closedir(dir);
}

------解决方案--------------------
探讨
struct dirent
{
long d_ino; /* inode number */
off_t d_off; /* offset to this dirent */
unsigned short d_reclen; /* length of this d_name */
char d_name [NAME_MAX+1]; /* filename (null-terminated) */
}

d_ino is an inode number. d_of…

------解决方案--------------------
<?php
//Open images directory
$dir = opendir("images");//List files in images directory
while (($file = readdir($dir)) !== false)
{
echo "filename: " . $file . "<br />";
}closedir($dir);
?>

The output of the code above could be:
上述代码将输出下面的结果:

filename: .
filename: ..
filename: cat.gif
filename: dog.gif
filename: food
filename: horse.gif