(二)和菜鸟一起学unix之文件和目录 opendir ,closedir

来源:互联网 发布:myeclipse mac版 编辑:程序博客网 时间:2024/06/01 13:27

man opendir:

NAME
       opendir - open a directory

SYNOPSIS

头文件
       #include <sys/types.h>
       #include <dirent.h>

       DIR *opendir(const char *name);

DESCRIPTION
       The  opendir()  function  opens a directory stream corresponding to the
       directory name, and returns a pointer to  the  directory  stream.   The
       stream is positioned at the first entry in the directory.

RETURN VALUE
       The  opendir()  function returns a pointer to the directory stream.  On
       error, NULL is returned, and errno is set appropriately.

ERRORS
       EACCES Permission denied.

功能:opendir用来打开参数name制定的目录,并返回DIR*形态的目录流
返回值:成功返回目录流,失败返回NULL

man closedir:

NAME
       closedir - close a directory

SYNOPSIS
       #include <sys/types.h>

       #include <dirent.h>

       int closedir(DIR *dir);

DESCRIPTION
       The  closedir()  function  closes  the directory stream associated with
       dir.  The directory stream descriptor dir is not available  after  this
       call.

功能关闭一个 dir 所指的mulu

RETURN VALUE
       The  closedir()  function  returns  0  on  success.   On  error,  -1 is
       returned, and errno is set appropriately.

返回值:成功返回0 错误返回-1

例子:

  1  #include<stdio.h>
  2 #include<sys/types.h>
  3 #include<dirent.h>
  4 int main(int argc ,char *argv[])
  5 {
  6     DIR *dir;
  7     if(( dir = opendir(".")) == 0)
  8         perror("open faile:");
  9     else
10        printf("opendir ok\n");
11     if((closedir(dir)) < 0)
12         perror("close falie");
13     else
14         printf("close ok\n");
15
16     return 0;
17
18 }