Unix環境高級編程(二)文件和目錄
本章主要介紹的是文件結構及目錄。重點是通過stat函數獲取文件的結構信息,然後是文件目錄及其遍歷。學完本章後,編寫了一個輸出給的目錄下的文件信息的程序。
首先是包含在<sys/stat.h>文件下的stat、fstat、lstat三個函數,三個函數的原型如下:
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,套接字(socket),符號鏈接(symbolic link)。
通過stat系類函數可以判斷一個文件是否存在,獲取文件的相關信息,例如文件類型、文件長度。
Unix中只有內核才能寫目錄,對某個目錄具有訪問權限的任一個用戶都可以讀該目錄。
在頭文件<dirent.h>中,提供有一系類目錄操作函數。
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 */
};
現在寫個小程序,鞏固函數的運用。程序的功能是:給定一個目錄,輸出該目錄下所有目錄及文件信息。程序如下:
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <sys/stat.h>
5 #include <dirent.h>
6 #include <string.h>
7 #include <errno.h>
8
9 void showallfile(char* filepath);
11 int main(int argc,char* argv[])
12 {
13 if(argc != 2)
14 {
15 printf("Error.Please input filepath.\n");
16 exit(-1);
17 }
18 //argv[1]中是目錄參數
19 showallfile(argv[1]);
20 return 0;
21 }
22
23 void showallfile(char* filepath)
24 {
25 struct stat st;
26 DIR *dp;
27 struct dirent* dirp;
28 char *pstr;
29 //獲取文件信息
30 if(lstat(filepath,&st) == -1)
31 {
32 perror("lstat() error");
33 exit(-1);
34 }
35 //判斷文件是否是目錄文件
36 if(S_ISDIR(st.st_mode) == 0) //不是
37 printf("File: %s\n",filepath);
38 else //是目錄文件,需要進行讀目錄操作
39 {
40 printf("Directory: %s\n",filepath);
41 pstr = filepath+strlen(filepath);
42 *pstr++ = ‘/‘;
43 *pstr = 0;
44 //打開目錄
45 if((dp = opendir(filepath)) == NULL)
46 {
47 printf("opendir() error");
48 exit(-1);
49 }
50 //讀取該目錄下的內容
51 while((dirp=readdir(dp))!= NULL)
52 {
53 if(strcmp(dirp->d_name,".") == 0 ||strcmp(dirp->d_name,"..") == 0)
54 continue;
55 strcpy(pstr,dirp->d_name);
56 //遞歸調用
57 showallfile(filepath);
58 }
59 }
60 }
程序測試結果:
輸出 /home/anker/Programs目錄下面所有的文件。
參考資料:http://linux.die.net/
Unix環境高級編程(二)文件和目錄