Linux下目录文件的操作(opendir,readdir,closedir) 以及DIR,dirent,stat等结构体详解

From:http://blog.chinaunix.net/uid-27213819-id-3810699.html

注:为什么要说 目录文件?其实在linux中 目录也是一种 文件,只是它的内容是上级的 目录和当前 目录下的 文件信息等,详情可以看看相关深入的书籍


opendir(打开目录

 
相关函数 open,readdir,closedir,rewinddir,seekdir,telldir,scandir
表头文件 #include<sys/types.h>
#include<dirent.h>
定义函数 DIR * opendir(const char * name);
函数说明 opendir()用来打开参数name指定的目录,并返回DIR*形态的目录流,和open()类似,接下来对目录的读取和搜索都要使用此返回值。
返回值 成功则返回DIR* 形态的目录流,打开失败则返回NULL。
错误代码 EACCESS 权限不足
EMFILE 已达到进程可同时打开的文件数上限。
ENFILE 已达到系统可同时打开的文件数上限。
ENOTDIR 参数name非真正的目录
ENOENT 参数name 指定的目录不存在,或是参数name 为一空字符串。
ENOMEM 核心内存不足。


readdir(读取目录


相关函数 open,opendir,closedir,rewinddir,seekdir,telldir,scandir
表头文件 #include<sys/types.h>
#include<dirent.h>
定义函数 struct dirent * readdir(DIR * dir);
函数说明 readdir()返回参数dir目录流的下个目录进入点。
结构dirent定义如下
struct dirent
{
    ino_t                 d_ino;
    ff_t                   d_off;
    signed short int d_reclen;
    unsigned char   d_type;
    har                   d_name[256];
};
d_ino      此目录进入点的inode
d_off       目录文件开头至此目录进入点的位移
d_reclen d_name的长度,不包含NULL字符
d_type    d_name 所指的文件类型
d_name  文件
返回值 成功则返回下个目录进入点。有错误发生或读取到目录文件尾则返回NULL。
附加说明 EBADF参数dir为无效的目录流。


closedir(关闭目录

 
相关函数 opendir
表头文件 #include<sys/types.h>
#include<dirent.h>
定义函数 int closedir(DIR *dir);
函数说明 closedir()关闭参数dir所指的目录流。
返回值 关闭成功则返回0,失败返回-1,错误原因存于errno 中。
错误代码 EBADF 参数dir为无效的目录
范例 参考readir()。


http://www.liweifan.com/2012/05/13/linux-system-function-files-operation/

最近在看Linux下文件操作相关章节,遇到了这么几个结构体,被搞的晕乎乎的,今日有空,仔细研究了一下,受益匪浅。

首先说说DIR这一结构体,以下为DIR结构体的定义:

  1. struct __dirstream
  2. {
  3.     void     *__fd;
  4.     char     *__data;
  5.     int        __entry_data;
  6.     char     *__ptr;
  7.     int       __entry_ptr;
  8.     size_t  __allocation;
  9.     size_t  __size;
  10.     __libc_lock_define (, __lock) 
  11. };
  12.       
  13. typedef struct __dirstream DIR;

DIR结构体类似于FILE,是一个内部结构,以下几个函数用这个内部结构保存当前正在被读取的目录的有关信息(摘自《UNIX环境高级编程(第二版)》)。函数 DIR *opendir(const char *pathname),即打开文件目录,返回的就是指向DIR结构体的指针,而该指针由以下几个函数使用:

  1. struct dirent *readdir(DIR *dp);
  2. void rewinddir(DIR *dp);
  3. int  closedir(DIR *dp);
  4. long telldir(DIR *dp);
  5. void seekdir(DIR *dp, long loc);

关于DIR结构,我们知道这么多就可以了,没必要去再去研究他的结构成员。

接着是dirent结构体,首先我们要弄清楚目录文件(directory file)的概念:这种文件包含了其他文件的名字以及指向与这些文件有关的信息的指针(摘自《UNIX环境高级编程(第二版)》)。从定义能够看出,dirent不仅仅指向目录,还指向目录中的具体文件,readdir函数同样也读取目录下的文件,这就是证据。以下为dirent结构体的定义:

 

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

从上述定义也能够看出来,dirent结构体存储的关于文件的信息很少,所以dirent同样也是起着一个索引的作用,如果想获得类似ls -l那种效果的文件信息,必须要靠stat函数了。

通过readdir函数读取到的文件名存储在结构体dirent的d_name成员中,而函数

int stat(const char *file_name, struct stat *buf);

的作用就是获取文件名为d_name的文件的详细信息,存储在stat结构体中。以下为stat结构体的定义:

  1. struct stat {
  2.             mode_t st_mode;     //文件访问权限
  3.             ino_t  st_ino;      //索引节点号
  4.             dev_t  st_dev;      //文件使用的设备号
  5.             dev_t  st_rdev;     //设备文件的设备号
  6.             nlink_t st_nlink;   //文件的硬连接数
  7.             uid_t  st_uid;      //所有者用户识别号
  8.             gid_t  st_gid;      //组识别号
  9.             off_t  st_size;     //以字节为单位的文件容量
  10.             time_t st_atime;    //最后一次访问该文件的时间
  11.             time_t st_mtime;     //最后一次修改该文件的时间
  12.             time_t st_ctime;     //最后一次改变该文件状态的时间
  13.             blksize_t st_blksize; //包含该文件的磁盘块的大小
  14.             blkcnt_t st_blocks;    //该文件所占的磁盘块
  15. };


这个记录的信息就很详细了吧,呵呵。

最后,总结一下,想要获取某目录下(比如a目下)b文件的详细信息,我们应该怎样做?
首先
,我们使用opendir函数打开目录a,返回指向目录a的DIR结构体c。
接着
,我们调用readdir(c)函数读取目录a下所有文件(包括目录),返回指向目录a下所有文件的dirent结构体d。

然后
,我们遍历d,调用stat(d->name,stat *e)来获取每个文件的详细信息,存储在stat结构体e中。

总体就是这样一种逐步细化的过程,在这一过程中,三种结构体扮演着不同的角色。

下面我们开发一个小程序,这个程序有一个参数.如果这个参数是一个文件名,我们输出这个文件的大小和最后修改的时间,如果是一个目录我们输出这个目录下所有文件的大小和修改时间.

  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <errno.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <dirent.h>
  7. #include <time.h>

  8. static int get_file_size_time(const char *filename) {
  9.     struct stat statbuf;
  10.     if (stat(filename, &statbuf) == -1) {
  11.         printf("Get stat on %s Error:%s\n",
  12.                 filename, strerror(errno));
  13.         return (-1);
  14.     }
  15.     if (S_ISDIR(statbuf.st_mode))return (1);
  16.     if (S_ISREG(statbuf.st_mode))
  17.         printf("%s size:%ld bytes\tmodified at %s",
  18.             filename, statbuf.st_size, ctime(&statbuf.st_mtime));
  19.     return (0);
  20. }

  21. int main(int argc, char **argv) {
  22.     DIR *dirp;
  23.     struct dirent *direntp;
  24.     int stats;
  25.     char buf[80];
  26.     if (argc != 2) {
  27.         printf("Usage:%s filename\n\a", argv[0]);
  28.         exit(1);
  29.     }
  30.     if (((stats = get_file_size_time(argv[1])) == 0) || (stats == -1))exit(1);
  31.     if ((dirp = opendir(argv[1])) == NULL) {
  32.         printf("Open Directory %s Error:%s\n",
  33.                 argv[1], strerror(errno));
  34.         exit(1);
  35.     }
  36.     while ((direntp = readdir(dirp)) != NULL){
  37.         sprintf(buf,"%s/%s",argv[1],direntp->d_name);
  38.         if (get_file_size_time(buf) == -1)break;
  39.     }
  40.     closedir(dirp);
  41.     exit(1);
  42. }
  43.         

//判断一个目录文件是否存在可以用c语言库里的Access函数

int _access( const char *path, int mode );

Return Value

Each of these functions returns 0 if the file has the given mode. The function returns –1 if the named file does not exist or is not accessible in the given mode; in this case, errno is set as follows:

EACCES

Access denied: file’s permission setting does not allow specified access.

ENOENT

Filename or path not found.

Parameters

path

File or directory path

mode

Permission setting

Remarks

When used with files, the _access function determines whether the specified file exists and can be accessed as specified by the value of mode. When used with directories, _access determines only whether the specified directory exists; in Windows NT, all directories have read and write access.

mode Value            Checks File For 
00                              Existence only 
02                              Write permission 
04                              Read permission 
06                              Read and write permission

http://www.liweifan.com/2011/12/19/embedded-system-s3c2440-sdram-link/    s3c2440 sdram
  1. /* ACCESS.C: This example uses _access to check the
  2.  * file named "ACCESS.C" to see if it exists and if
  3.  * writing is allowed.
  4.  */

  5. #include <io.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>

  8. void main( void )
  9. {
  10.    /* Check for existence */
  11.    if( (_access( "ACCESS.C", 0 )) != -1 )
  12.    {
  13.       printf( "File ACCESS.C exists " );
  14.       /* Check for write permission */
  15.       if( (_access( "ACCESS.C", 2 )) != -1 )
  16.          printf( "File ACCESS.C has write permission " );
  17.    }
  18. }


你可能感兴趣的:(Linux下目录文件的操作(opendir,readdir,closedir) 以及DIR,dirent,stat等结构体详解)