解析linux 文件和目錄操作的相關(guān)函數(shù)
struct stat
{
mode_t st_mode; 文件類型,文件權(quán)限
ino_t st_ino; i節(jié)點號
dev_t st_dev;
dev_t st_rdev; 設(shè)備文件序號
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; 文件內(nèi)容的最后修改時間
time_t st_ctime; 文件狀態(tài)的最后修改時間
long st_blksize;
long st_blocks;
};
1,stat函數(shù)取得文件信息。
#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函數(shù)類似于stat,但是當命名的文件是一個符號連接時,lstat返回該符號連接的有關(guān)信息,而不是由該符號連接引用的文件的信息
2,access函數(shù)判斷文件權(quán)限
#include<unistd.h>
int access (const char *name, int mode) ;
返回:若成功則為 0,若出錯則為- 1
access函數(shù)的mode常數(shù),取自 <unistd.h>
mode 說 明
R_OK 測試讀許可權(quán)
W_OK 測試寫許可權(quán)
X_OK 測試執(zhí)行許可權(quán)
F_OK 測試文件是否存在
3,umask函數(shù)設(shè)置文件創(chuàng)建屏蔽字
#include <sys/types.h>
#include <sys/stat.h>
mode_t umask(mode_t task) ;
返回:以前的文件方式創(chuàng)建屏蔽字
4,chmod函數(shù)用于修改文件的權(quán)限
#include <sys/types.h>
#include <sys/stat.h>
int chmod(const char *pathname, mode_t mode);
int fchmod(int fd, mode_t mode);
兩個函數(shù)返回:若成功則為 0,若出錯則為- 1
5,chown函數(shù)可用于更改文件的用戶 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);
三個函數(shù)返回:若成功則為 0,若出錯則為- 1
6,在文件末尾處截短文件可以調(diào)用函數(shù) 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) ;
兩個函數(shù)返回;若成功則為 0,若出錯則為- 1
7,創(chuàng)建一個向現(xiàn)存文件連接的方法是使用link函數(shù),想當于硬連接 ln。只有超級用戶進程可以創(chuàng)建指向一個目錄的新連接。其理由是這樣做可能在文件系統(tǒng)中形成循環(huán),大多數(shù)處理文件系統(tǒng)的公用程序都不能處理這種情況
#include <unistd.h>
int link(const char*oldpath, const char *newpath) ;
返回:若成功則為 0,若出錯則為- 1
為了刪除一個現(xiàn)存的目錄項,可以調(diào)用 unlink函數(shù)。
#include <unistd.h>
int unlink(const char *pathname) ;
返回:若成功則為 0,若出錯則為-1。此函數(shù)刪除目錄項,并將由 pathname所引用的文件的連接計數(shù)減 1。
硬連接的一些限制: ( a )硬連接通常要求連接和文件位于同一文件系統(tǒng)中, ( b )只有超級用戶才能創(chuàng)建到目錄的硬連接。
symlink函數(shù)創(chuàng)建一個符號連接。相當于軟連接,ln -s
#include <unistd.h>
int symlink(const char *oldpath, const char *sympath) ;
返回:若成功則為 0,若出錯則為- 1
因為open函數(shù)跟隨符號連接,所以需要有一種方法打開該連接本身,并讀該連接中的名字。
readlink函數(shù)提供了這種功能。
#include <unistd.h>
int readlink(const char *pathname, char *buf, int bufsize) ;
返回:若成功則為讀的字節(jié)數(shù),若出錯則為- 1
此函數(shù)組合了open, read和close的所有操作。
8,用mkdir函數(shù)創(chuàng)建目錄,用 rmdir函數(shù)刪除目錄。
#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函數(shù)解除對一個文件或目錄的連接。對于文件, remove的功能與unlink相同。對于目錄, remove的功能與rmdir相同。
#include <stdio.h>
int remove(const char *pathname) ;
返回:若成功則為 0,若出錯則為- 1
文件或目錄用rename函數(shù)更名。
#include <stdio.h>
int rename(const char *oldname, const char *newwname) ;
返回:若成功則為 0,若出錯則為- 1
10,一個文件的存取和修改時間可以用 utime函數(shù)更改。
#include <sys/types.h>
#include <utime.h>
int utime (const char *name, const struct utimebuf *t);
返回:若成功則為 0,若出錯則為- 1
如果times是一個空指針,則存取時間和修改時間兩者都設(shè)置為當前時間;
如果times是非空指針,則存取時間和修改時間被設(shè)置為 times所指向的結(jié)構(gòu)中的值。此時,進程的有效用戶ID必須等于該文件的所有者 ID,或者進程必須是一個超級用戶進程。對文件只具有寫許可權(quán)是不夠的
此函數(shù)所使用的結(jié)構(gòu)是:
struct utimbuf {
time_t actime; /*access time*/
time_t modtime; /*modification time*/
}
11,對文件目錄的操作函數(shù),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結(jié)構(gòu)與實現(xiàn)有關(guān)。 此結(jié)構(gòu)至少包含下列兩個成員:
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為存儲路徑的字符數(shù)組,size為長度
上一篇:c語言中malloc、realloc與calloc 的區(qū)別以及聯(lián)系
欄 目:C語言
下一篇:C語言位運算符:與、或、異或、取反、左移與右移詳細介紹
本文標題:解析linux 文件和目錄操作的相關(guān)函數(shù)
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/4272.html
您可能感興趣的文章
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計- 解析最少換車次數(shù)的問題詳解
- 01-10fatal error LNK1104: 無法打開文件“l(fā)ibc.lib”的解決方法
- 01-10深入解析最長公共子串
- 01-10顯示任何進程加載的DLL文件的代碼
- 01-10深入解析Linux下\r\n的問題
- 01-10Linux線程管理必備:解析互斥量與條件變量的詳解
- 01-10Linux C 獲取進程退出值的實現(xiàn)代碼
- 01-10解析Linux下的時間函數(shù):設(shè)置以及獲取時間的方法
- 01-10深入探討linux下進程的最大線程數(shù)、進程最大數(shù)、進程打開的文
- 01-10DHCP:解析開發(fā)板上動態(tài)獲取ip的2種實現(xiàn)方法詳解


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實例總結(jié)
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改


