第四章主要介绍的是文件结构及目录。重点是通过stat函数获取文件的结构信息,然后是文件目录及其遍历。学完本章后,编写了一个输出给的目录下的文件信息的程序。
首先是包含在
int stat(const char *path, struct stat *buf);
int fstat(int fd, struct stat *buf);
int lstat(const char *path, struct stat *buf);
三个函数的的返回值:成功返回0,出错返回-1。
三个函数的区别如下:
stat() stats the file pointed to by path and fills in buf.
lstat() is identical to stat(), except that if path is a symbolic link, then the link itself is stat-ed, not the file that it refers to.
fstat() is identical to stat(), except that the file to be stat-ed is specified by the file descriptor fd.
第二个参数buf是个指针,指向一个文件的具体信息。
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for file system I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
Unix下的文件类型有:普通文件(regular file),目录文件(directory file),快特殊文件(block special file),字符特殊文件(character special file),FIFO,套接字,符号链接(symbolic link)。
通过stat系类函数可以判断一个文件是否存在,获取文件的相关信息,例如文件类型,文件长度等等。
Unix中只有内核才能写目录,对某个目录具有访问权限的任一个用户都可以读该目录。
在头文件
DIR *opendir(const char *name);
struct dirent *readdir(DIR *dirp);
void rewinddir(DIR *dirp);
int closedir(DIR *dirp);
dirent结构如下:
struct dirent {
ino_t d_ino; /* inode number */
off_t d_off; /* offset to the next dirent */
unsigned short d_reclen; /* length of this record */
unsigned char d_type; /* type of file; not supported
by all file system types */
char d_name[256]; /* filename */
};
现在写个小程序,巩固函数的运用。程序的功能是:给定一个目录,输出该目录下所有目录及文件信息,程序如下:
#include
#include
#include
#include
#include
#include
#include
void ShowAllFile(char* FilePath);
int main(int argc, char* argv[])
{
if (argc != 2)
{
//需要输入两个参数
printf("Error! Please input FilePath.\n");
exit(-1);//
}
//argv[1]中是目录参数
ShowAllFile(argv[1]);
return 0;
}
void ShowAllFile(char* FilePath)
{
struct stat st;
DIR * dp;
struct dirent* dirp;
char *pstr;
//进行文件信息的获取
if (lstat(FilePath, &st) == -1)
{
perror("lstat() error");
exit(-1);
//判断文件是否是目录文件
if (S_ISDIR(st.st_mode) == 0)//显然不是
{
printf("File:%s\n", FilePath);
}
else
{
printf("Directory: %s\n", FilePath);
pstr = FilePath + strlen(FilePath);
*pstr++ = '/';
*pstr = 0;
//打开目录
if (dp = opendir(FilePath) == NULL)
{
printf ("opendir() error");
exit(-1);
}
//读取该目录下的内容
while ((dirp = readdir(dp)) != NULL)
{
//读到该目录和上层目录
if (strcmp(dirp->d_name, ".") == 0) || (strcmp(dirp->d_name, "..") == 0)
continue;
strcpy(pstr, dirp->d_name);
//递归调用
ShowAllFile(FilePath);
}
}
}
}
程序测试结果如下: