| 关键词: nbsp 文件 DIR include 函数 char 目录 const stat struct |
1.stat函数 stat函数返回一个与此命名文件有关的信息结构,fstat函数获得已在描述符filedes上打开的文件的有关信息。lstat函数类似于stat,但是当命名的文件是一个符号连接时,lstat返回该符号连接的有关信息,而不是由该符号连接引用的文件的信息。view plaincopy to clipboardprint?#include <sys/types.h> #include <sys/stat.h> int stat(const char *pathname, struct stat *buf) ; int fstat(int filedes,struct stat *buf) ; int lstat(const char *pathname, struct stat *buf) ; 三个函数的返回:若成功则为0,若出错则为-1stat的数据结构如下所示view plaincopy to clipboardprint?struct stat { mode_t st_mode; /* 文件类型和权限*/ ino_t st_ino; /* inode 号*/ nlink_t st_nlink; /* 链接数目*/ uid_t st_uid; /* 文件所有者的uid*/ gid_t st_gid; /* 文件所有者的组id */ off_t st_size; /* 文件大小*/ time_t st_atime; /* 最后访问时间*/ time_t st_mtime; /* 最后修改时间*/ time_t st_ctime; /* 最后文件状态修改时间*/ blksize_t st_blksize; /* 最佳读写I/O块大小*/ blkcnt_t st_blocks; /* 一共有多少个块*/ }; 2.access函数 access按实际用户ID和实际组ID进行文件权限的测试。view plaincopy to clipboardprint?#include <unistd.h> int access(const char *pathname, int mode) ; 返回:若成功则为0,若出错则为-1参数解析: mode值可以是以下几种: R_OK 测试读许可权 W_OK 测试写许可权 X_OK 测试执行许可权 F_OK 测试文件是否存在3.truncate函数truncate 把某个文件截短到一定长度,跟open中的O_TRUNC类似。view plaincopy to clipboardprint?int truncate(const char *pathname, off_t length); int ftruncate(int filedes, off_t length); 成功返回0 ,失败返回-14.与链接有关的函数link函数用来创建一个硬链接文件 int link(const char *existingpath, const char *newpath);symlink用来创建一个软链接文件 int symlink(const char *actualpath, const char *sympath);unlink删除软链接时只删除链接文件本身,被连接的文件不受影响。删除硬链接时,如果inode引用数为0,则删除文件。如果有进程还在使用该文件,则可以继续使用。 int unlink(const char *pathname);readlink只读取软连接本身 ssize_t readlink(const char* restrict pathname, char *restrict buf, size_t bufsize);5.utime函数utime读取文件的最后访问时间和修改实际那view plaincopy to clipboardprint?#include <utime.h> int utime(const char *pathname, const struct utimbuf *times); struct utimbuf { time_t actime; /* 最后访问时间*/ time_t modtime; /* 最后修改时间*/ } Inode属性的修改时间有内核来完成,应用程序没有权限去设置。6.目录操作函数opendir用来打开一个目录 的内容,并返回目录指证。#include <dirent.h>DIR *opendir(const char *pathname); 成功返回DIR指针,失败返回NULL readdir 以一定次序读取目录内容, struct dirent *readdir(DIR *dp); struct dirent { ino_t d_ino; /* i-node 号*/ char d_name[NAME_MAX + 1]; /*文件或目录名*/ }rewinddir 重置目录的输入流,重置后从第一个目录项开始读。void rewinddir(DIR *dp);telldir 返回目录流的当前位置long telldir(DIR *dp);seekdir 设置目录流位置。void seekdir(DIR *dp, long loc);loc有telldir返回closedir 关闭目录流int closedir(DIR *dp);下面的例子是递归遍历一个目录,打印出这个目录下所有的文件和目录,并且需要打印出这些文件的类型(比如 链接文件,普通文件等)。view plaincopy to clipboardprint?#include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <sys/stat.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <dirent.h> void iterate_dir( char *dir ); int main(int argc, char **argv) { if( argc != 2 ){ printf("usage: command dir/n"); return -1; } iterate_dir( argv[1] ); return 0; } void iterate_dir ( char * dir ) { DIR *cur_dir; struct dirent *dir_ent; struct stat dir_stat; char path[PATH_MAX]; cur_dir = opendir(dir); if ( NULL == cur_dir ){ printf("open dir faild %s/n", strerror(errno)); return ; } while( NULL != ( dir_ent=readdir(cur_dir))){ if ( 0==strcmp(dir_ent->d_name,".") || 0==strcmp( dir_ent->d_name,"")){ continue; } sprintf( path, "%s%s",dir,dir_ent->d_name); if ( -1 == lstat(path, &dir_stat) ){ printf("lstat error %s /n",strerror(errno)); return ; } if ( S_ISDIR( dir_stat.st_mode) ){ strcat(path,"/"); printf("path: %s/n",path); iterate_dir(path); continue; } printf("file: %s /n", path); } } |
|
声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系
[邮箱地址] 删除
|