linux 文件和目錄操作的相關函數

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和組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是一個空指針,則存取時間和修改時間兩者都設置爲當前時間;
如果times是非空指針,則存取時間和修改時間被設置爲 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爲長度

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章