(1)普通文件(regular file):這是最常用的文件類型,這種文件包含了某種形式的數據,文件內容的解釋由處理該文件的應用程式進行。 (2)目錄文件(directory file):這種文件包含了其他文件的 名字以及指向這些文件有關信息的指針。對一個目錄文件具有讀許可權的進程,都可以讀該目錄的內 ...
(1)普通文件(regular file):這是最常用的文件類型,這種文件包含了某種形式的數據,文件內容的解釋由處理該文件的應用程式進行。
(2)目錄文件(directory file):這種文件包含了其他文件的 名字以及指向這些文件有關信息的指針。對一個目錄文件具有讀許可權的進程,都可以讀該目錄的內容,但只有內核可以 直接寫目錄文件。
(3)塊特殊文件(block special file):這種類型的文件提供對設備(如磁碟)帶緩衝的訪問,每次訪問一固定長度為單位進行。
(4)字元特殊文件(character special file):這種類型的文件提供對設備不帶緩衝的訪問,每次訪問長度可變。系統中的所有設備,要麼是字元特殊文件,要麼是塊特殊文件。
(5)FIFO:這種文件用於進程間通信,也叫做有命名管道。
(6)套接字(socket):這種類型的文件用於進程間的網路通信。套接字也可用於一臺宿主機上進程間的非網路通信。
(7)符號連接(symbolic link):這種類型的文件指向另一個文件。
函數描述:
1、頭文件:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
2、函數原型:
int stat(const char *path, struct stat *buf);
int lstat(const char *path, struct stat *buf);
3、參數:
a、path:文件名
b、buf:是一個(struct stat)的指針
下麵是struct stat結構體的詳細介紹:
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 */ };
4、返回值:
成功:0
失敗:-1
總結:這裡stat和lstat有點區別的,lstat可以看到符號連接(symbokic link),而stat卻不可以,下麵我用代碼具體給出來區別的地方。
文件類型包括在stat結構的st_mode成員中,下麵是這7種文件類型的判斷方法:
巨集 文件類型
S_ISREG(m) 普通文件(is it a regular file?)
S_ISDIR(m) 目錄文件(directory?)
S_ISCHR(m) 字元特殊文件(character device?)
S_ISBLK(m) 塊特殊文件(block device?)
S_ISFIFO(m) 管道或FIFO [FIFO (named pipe)?]
S_ISLNK(m) 符號鏈接 [symbolic link? (Not in POSIX.1-1996.)]
S_ISSOCK(m) 套接字 [socket? (Not in POSIX.1-1996.)]
判斷例子:
struct stat fileinfo; stat("text.txt",&fileinfo); //text.txt為普通文件 if(S_ISREG(fileinfo.st_mode)) { printf("the file is regular file\n"); }
好了,說了這麼多,直接上代碼看看具體是怎麼判斷文件的類型的:
#include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc,char *argv[]) { if(argc !=5 ) { printf("argc must equal to 5!\n"); exit(1); } struct stat fileinfo; int i; for(i=1;i<5;i++) { bzero(&fileinfo,sizeof(fileinfo)); //stat(argv[i],&fileinfo); //這裡stat和lstat有點的區別,lstat可以產看到符號連接(symbokic link),而stat卻不可以 lstat(argv[i],&fileinfo); if(S_ISREG(fileinfo.st_mode)) //普通文件 { printf("%s file is regular file\n",argv[i]); } else if(S_ISDIR(fileinfo.st_mode)) //目錄 { printf("%s file is directory file\n",argv[i]); } else if(S_ISCHR(fileinfo.st_mode)) //字元特殊文件 { printf("%s file is character device file\n",argv[i]); } else if(S_ISBLK(fileinfo.st_mode)) //塊特殊文件 { printf("%s file is block device file\n",argv[i]); } else if(S_ISFIFO(fileinfo.st_mode)) //FIFO或管道 { printf("%s file is FIFO\n",argv[i]); } else if(S_ISLNK(fileinfo.st_mode)) //符號連接 { printf("%s file is symbokic link\n",argv[1]); } else if(S_ISSOCK(fileinfo.st_mode)) //套接字 { printf("%s file is socket\n",argv[i]); } else { printf(" %s not a file name\n",argv[i]); } } return 0; }
lstat和stat的區別:
代碼中註釋掉://stat(argv[i],&fileinfo); 即是用到lstat函數時終端的輸出如下
代碼中註釋掉:lstat(argv[i],&fileinfo);即是用到stat函數終端的輸出如下