struct stat
{
mode_t st_mode; 檔案類型,檔案許可權
ino_t st_ino; i節點號
dev_t st_dev;
dev_t st_rdev; 裝置檔案序號
nlink_t st_nlink; 連結
uid_t st_uid;
gid_t st_gid; 使用者ID
off_t st_size; 檔案大小,此欄位只對普通檔案、目錄檔案和符號串連有意義。
time_t st_atime; 最後存取時間
time_t st_mtime; 檔案內容的最後修改時間
time_t st_ctime; 檔案狀態的最後修改時間
long st_blksize;
long st_blocks;
};
1,stat函數取得檔案資訊。
#include <sys/types.h>
#include <sys/stat.h>
int stat(const char *pathname, struct stat *buf);
int fstat (int fd,struct stat *buf);
int lstat(const char *pathname, struct stat *buf);
lstat函數類似於stat,但是當命名的檔案是一個符號串連時,lstat返回該符號串連的有關資訊,而不是由該符號串連引用的檔案的資訊
2,access函數判斷檔案許可權
#include<unistd.h>
int access (const char *name, int mode) ;
返回:若成功則為 0,若出錯則為- 1
access函數的mode常數,取自 <unistd.h>
mode 說 明
R_OK 測試讀許可權
W_OK 測試寫許可權
X_OK 測試執行許可權
F_OK 測試檔案是否存在
3,umask函數設定檔案建立屏蔽字
#include <sys/types.h>
#include <sys/stat.h>
mode_t umask(mode_t task) ;
返回:以前的檔案方式建立屏蔽字
4,chmod函數用於修改檔案的許可權
#include <sys/types.h>
#include <sys/stat.h>
int chmod(const char *pathname, mode_t mode);
int fchmod(int fd, mode_t mode);
兩個函數返回:若成功則為 0,若出錯則為- 1
5,chown函數可用於變更檔的使用者識別碼和組ID。
#include <sys/types.h>
#include <unistd.h>
int chown(const char *pathname,uid_t owner,gid_t group);
int fchown(int fd, uid_t owner, gid_t group);
int lchown(const char *pathname, uid_t owner, gid_t group);
三個函數返回:若成功則為 0,若出錯則為- 1
6,在檔案末尾處截短檔案可以調用函數 truncate和ftruncate。將一個檔案的長度截短為 0是一個特例,用O_TRUNC標誌可以做到這一點。
#include <sys/types.h>
#include <unistd.h>
int truncate(const char *pathname, off_t
length) ;
int ftruncate(int filedes, off_t length) ;
兩個函數返回;若成功則為 0,若出錯則為- 1
7,建立一個向現存檔案串連的方法是使用link函數,想當於硬串連 ln。只有超級使用者進程可以建立指向一個目錄的新串連。其理由是這樣做可能在檔案系統中形成迴圈,大多數處理檔案系統的公用程式都不能處理這種情況
#include <unistd.h>
int link(const char*oldpath, const char *newpath) ;
返回:若成功則為 0,若出錯則為- 1
為了刪除一個現存的目錄項,可以調用 unlink函數。
#include <unistd.h>
int unlink(const char *pathname) ;
返回:若成功則為 0,若出錯則為-1。此函數刪除目錄項,並將由 pathname所引用的檔案的串連計數減 1。
硬串連的一些限制: ( a )硬串連通常要求串連和檔案位於同一檔案系統中, ( b )只有超級使用者才能建立到目錄的硬串連。
symlink函數建立一個符號串連。相當於軟串連,ln -s
#include <unistd.h>
int symlink(const char *oldpath, const char *sympath) ;
返回:若成功則為 0,若出錯則為- 1
因為open函數跟隨符號串連,所以需要有一種方法開啟該串連本身,並讀該串連中的名字。
readlink函數提供了這種功能。
#include <unistd.h>
int readlink(const char *pathname, char *buf, int bufsize) ;
返回:若成功則為讀的位元組數,若出錯則為- 1
此函數組合了open, read和close的所有操作。
8,用mkdir函數建立目錄,用 rmdir函數刪除目錄。
#include <sys/types.h>
#include <sys/stat.h>
int mkdir(const char *pathname, mode_t mode) ;
返回:若成功則為 0,若出錯則為- 1
#include <unistd.h>
int rmdir(const char *pathname) ;
返回:若成功則為 0,若出錯則為 - 1
9,remove函數解除對一個檔案或目錄的串連。對於檔案, remove的功能與unlink相同。對於目錄, remove的功能與rmdir相同。
#include <stdio.h>
int remove(const char *pathname) ;
返回:若成功則為 0,若出錯則為- 1
檔案或目錄用rename函數更名。
#include <stdio.h>
int rename(const char *oldname, const char *newwname) ;
返回:若成功則為 0,若出錯則為- 1
10,一個檔案的存取和修改時間可以用 utime函數更改。
#include <sys/types.h>
#include <utime.h>
int utime (const char *name, const struct utimebuf *t);
返回:若成功則為 0,若出錯則為- 1
如果times是一個null 指標,則存取時間和修改時間兩者都設定為目前時間;
如果times是非null 指標,則存取時間和修改時間被設定為 times所指向的結構中的值。此時,進程的有效使用者ID必須等於該檔案的所有者 ID,或者進程必須是一個超級使用者進程。對檔案只具有寫許可權是不夠的
此函數所使用的結構是:
struct utimbuf {
time_t actime; /*access time*/
time_t modtime; /*modification time*/
}
11,對檔案目錄的操作函數,opendir readdir rewinddir
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *pathname) ;
返回:若成功則為指標,若出錯則為 NULL
struct dirent *readdir(DIR *dr);
返回:若成功則為指標,若在目錄尾或出錯則為 NULL
void rewinddir(DIR *dr);
重設讀取目錄的位置為開頭
int close(DIR *dr); 返回:若成功則為 0,若出錯則為- 1
定義在標頭檔<dirent.h>中的dirent結構與實現有關。 此結構至少包含下列兩個成員:
struct dirent {
ino_t d_ino;
char d_name[NAME_MAX+1];
}
12,chdir,改變目前的目錄
#include<unistd.h>
int chdir(const char *pathname);
int pchdir(int fd);
getcwd,得到目前的目錄的完整路徑.
#include<unistd.h>
char *getcwd(char *buf, size_t size);
若失敗返回NULL, buf為儲存路徑的字元數組,size為長度