Chinaunix首页 | 论坛 | 博客
  • 博客访问: 253059
  • 博文数量: 99
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 246
  • 用 户 组: 普通用户
  • 注册时间: 2013-05-03 18:23
个人简介

qrasvasdf

文章分类

全部博文(99)

文章存档

2016年(1)

2015年(36)

2014年(62)

我的朋友

分类: C/C++

2014-10-25 17:22:17

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

 

opendir(打开目录)


相关函数 open,readdir,closedir,rewinddir,seekdir,telldir,scandir
表头文件 #include
#include
定义函数 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
#include
定义函数 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 _name的长度,不包含NULL字符
d_type d_name 所指的文件类型
d_name 文件名

返回值 成功则返回下个目录进入点。有错误发生或读取到目录文件尾则返回NULL。
附加说明 EBADF参数dir为无效的目录流。

 

closedir(关闭目录)


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

 

下面我们开发一个小程序,这个程序有一个参数.如果这个参数是一个文件名,我们输出这

个文件的大小和最后修改的时间,如果是一个目录我们输出这个目录下所有文件的大小和

修改时间.

点击(此处)折叠或打开

  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


点击(此处)折叠或打开

  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. }


阅读(1997) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~