常見函式:
1,建立資料夾:mkdir
包含標頭檔案:
#include
函式格式:
int mkdir(const char *pathname, mode_t mode);
說明:
建立一個資料夾,資料夾的路徑是pathname,資料夾的許可權是mode,返回值,成功為0,不成為-1;
2,刪除資料夾:rmdir;
包含標頭檔案:
#include
函式格式:
int rmdir(const char *pathname);
說明:
刪除一個資料夾,資料夾的路徑是pathname,返回值,成功為0,不成為-1;
3,獲取當前路徑
包含標頭檔案:
#include
函式格式:
char *getcwd(char *buf, size_t size);char *getwd(char *buf);char *get_current_dir_name(void);
說明:
將當前檔案的路徑以字串形式返回,需要的引數分別是,檔名(字串),檔名的大小自己規定size位元組;
4,修改當前路徑,類似cd指令:chdir
包含標頭檔案:
#include
函式格式:
int chdir(const char *path);
說明:
將當前工作路徑修改為傳入的引數對應的路徑;
5,目錄開啟函式:opendir
包含標頭檔案:
#include
函式格式:
DIR *opendir(const char *name);
說明:
返回一個DIR型別的指標,指向開啟的資料夾,開啟失敗是返回NULL;
6,目錄讀函式:readdir
包含標頭檔案:
#include
函式格式:
struct dirent *readdir(DIR *dirp);
說明:
從開啟的DIR指標讀取目錄裡的內容,讀取失敗或者讀取到資料夾末尾時返回NULL,成功返回的是一個dirent型別的結構體指標,結構體的元素如下:
struct dirent2 {3 long d_ino; /* inode number 索引節點號 */4 off_t d_off; /* offset to this dirent 在目錄檔案中的偏移 ,第幾個檔案得意思*/5 unsigned short d_reclen; /* length of this d_name 檔名長 */6 unsigned char d_type; /* the type of d_name 檔案型別 */7 char d_name [NAME_MAX+1]; /* file name (null-terminated) 檔名,最長255字元 */8 }其中d_type元素有以下定義,是在標頭檔案定義好的整形數字DT_BLK This is a block deviceDT_CHR This is a character device//。DT_DIR This is a directory。DT_FIFO This is a named pipe (FIFO)。DT_LNK This is a symbolic link。DT_REG This is a regular file。DT_SOCK This is a UNIX domain socket。DT_UNKNOWN The file type is unknown。
7,關閉資料夾:close
包含標頭檔案:
#include
函式格式:
int closedir(DIR *dirp);
說明:
關閉對應指標指向的資料夾;
下邊是對資料夾操作的應用案例:
遍歷查詢指定目錄下的所有。c檔案並輸出到顯示器#include