您的位置:首页 > 运维架构

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

2012-10-05 11:53 387 查看
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 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息