Summary Day33

1. 文件管理

1,1 fcntl  函數

(1)F_SETLKW

功能與F_SETLK類似,所不同的是加不上鎖並不是返回失敗而是等待,直到可以加上該鎖爲止

(2)F_GETLK

表示試圖將第三個參數描述的鎖加到第一的參數指定的文件上如果能加上鎖但不會去加,而是

將該鎖的類型改爲F_UNLCK;如果不能加上鎖,則將文件中已經存在的鎖信息通過第三個參數帶出來,

並且將給文件進行加鎖的進程號設置到第三個參數的l_pid中


1.2 stat/fstat函數

#include <sys/types.h>

#include <sys/stat.h>

#include <unisth.h>

int stat(const char *path, struct stat *buf);

int fstat(int fd, struct stat *buf);

int lstat (const char *path, stuct stat *buf);

第一個參數:文件的路勁/文件的描述符

第二個參數:結構體指針,結構體變量的地址

struct stat {

...

mode_t st_mode;//文件的類型和權限

off_t st_size;//文件的大小

time_t  st_mtime;//文件的最後一次修改時間

...

};

函數的功能:主要用於獲取文件的詳細信息


類型查找:gcc/cc  -E xxx.c -o xxx.i ,在xxx.i中查找

mode_t undigned int

off_t long int

time_t long int


擴展:

#include <time.h>

char *ctime(const time_t *timep);

=>主要用於將整數類型的時間轉換爲字符串類型的時間

struct tm *location(const time_t *timpe);

=>主要用於將整數類型轉換成結構體類型

struct tm {

int tm_sec; //秒數

int tm_min;//分鐘

int tm_hour;//小時

Int tm_mady;//日

int tm_mon;//月+1

int tm_year;//年+1900

int tm_wday;//星期+1

int tm_yday;//年中的天數

int tm_isdst;//夏令時

};


1.3 access函數

#include <unistd.h>

int access(const char *pathname, int mode);

第一個參數:文件的路勁是否存在;

第二個參數:操作模式

函數的功能:

主要用於判斷文件是否存在以及是否擁有指定的權限


1.4 chmod和truncate

#include <sys/stat.h>

int chmod(const char*path, mode_t mode);

int fchmode(int fd, mode_t mode);

第一個參數:文件的路勁、文件描述符

第二個參數:文件新權限如:0644

函數的功能:主要用於修改文件的權限


#include <unistd.h>

#include <sys/types.h>

int truccate(const char *path, off_t  length);

int ftrucate(int fd, off_t length);

第一個參數:文件的路勁、文件的描述符

第二個參數:文件的最新大小

函數的 功能:主要用於將指定文件大小截取指定大小

1.6 mmap/munmap函數


2. 目錄管理

2.1 opendir()函數.

#Include <sys/type.h>

#Include <dirent.h>

DIR *opendir(const char *name);

DIR *fdopendir(int fd);

函數的功能:

主要用於打開參數指定的目錄,成功返回指針,失敗返回NULL

2.2 readdir函數

#include <dirent.h>

struct sirent *readdir(DIR *dirp);

函數功能:主要用於讀取參數指定的目錄中內容,成功返回結構體指針,失敗返回NULL

2.3 closedir 函數

#include <sys/types.h>

#Include <dirent.h>

int closedir (DIR *dirp)

函數的功能:主要用於關閉參數指定目錄

2.4 getcwd函數

#Include <unistd.h>

char *getcwd(char *buf, size_t size);

函數功能:

主要用於獲取當前工作目錄的絕對路徑存放到buf中,buf的大小是size個字節

成功返回buf,失敗返回NULL

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