解析linux 文件和目錄操作的相關函數(shù)
struct stat
{
mode_t st_mode; 文件類型,文件權限
ino_t st_ino; i節(jié)點號
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; 文件內(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返回該符號連接的有關信息,而不是由該符號連接引用的文件的信息
2,access函數(shù)判斷文件權限
#include<unistd.h>
int access (const char *name, int mode) ;
返回:若成功則為 0,若出錯則為- 1
access函數(shù)的mode常數(shù),取自 <unistd.h>
mode 說 明
R_OK 測試讀許可權
W_OK 測試寫許可權
X_OK 測試執(zhí)行許可權
F_OK 測試文件是否存在
3,umask函數(shù)設置文件創(chuàng)建屏蔽字
#include <sys/types.h>
#include <sys/stat.h>
mode_t umask(mode_t task) ;
返回:以前的文件方式創(chuàng)建屏蔽字
4,chmod函數(shù)用于修改文件的權限
#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是一個空指針,則存取時間和修改時間兩者都設置為當前時間;
如果times是非空指針,則存取時間和修改時間被設置為 times所指向的結構中的值。此時,進程的有效用戶ID必須等于該文件的所有者 ID,或者進程必須是一個超級用戶進程。對文件只具有寫許可權是不夠的
此函數(shù)所使用的結構是:
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結構與實現(xiàn)有關。 此結構至少包含下列兩個成員:
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為長度
- linux shell自定義函數(shù)(定義、返回值、變量作用域)介紹
- Linux里awk中split函數(shù)的用法小結
- linux下C語言中的mkdir函數(shù)與rmdir函數(shù)
- 解析Linux下的時間函數(shù):設置以及獲取時間的方法
- linux系統(tǒng)上支持php的 iconv()函數(shù)的方法
- PHP執(zhí)行l(wèi)inux系統(tǒng)命令的常用函數(shù)使用說明
- linux下access函數(shù)的用法介紹
- Linux Shell腳本系列教程(四):使用函數(shù)添加環(huán)境變量
- linux中常用腳本和函數(shù)分享
- linux下system函數(shù)的簡單分析
相關文章
使用C語言順序表數(shù)據(jù)結構實現(xiàn)棧的代碼示例
這篇文章主要給大家介紹了如何使用C語言順序表數(shù)據(jù)結構實現(xiàn)棧,文章通過代碼示例介紹的非常詳細,對大家的學習或工作有一定的參考價值,需要的朋友可以參考下2023-09-09