Linxu目錄操作相關函式(詳解)

2021-10-23 01:30:25 字數 3328 閱讀 2671

//標頭檔案

#include

//原型

intchdir

(const

char

*path)

;int

fchdir

(int fd)

;//功能:chdir函式用於改變當前工作目錄。

//返回值: 如果返回0, 代表成功。如果返回-1代表讀取不成功,錯誤**存入errno中

//引數:

//path 改變到的目錄

//標頭檔案

#include

//原型

char

*getcwd

(char

*buf, size_t size)

;char

*getwd

(char

*buf)

;char

*get_current_dir_name

(void);

//功能:getcwd()會將當前的工作目錄絕對路徑複製到引數buf 所指的記憶體空間,引數size 為buf 的空間大小。

//返回值:當前的工作目錄絕對路徑

//引數:

所指的記憶體空間

為buf 的空間大小。

舉例

#include

#include

#include

#include

#include

#include

intmain

(int argc,

char

* ar**)

int ret =

chdir

(ar**[1]

);if(ret ==-1

)int fd =

open

("chdir.txt"

, o_creat | o_rdwr,

0777);

if(fd ==-1

)close

(fd)

;char buf[

128]

;getcwd

(buf,

sizeof

(buf));

printf

("current dir: %s\n"

, buf)

;return0;

}

//標頭檔案

#include

#include

//原型

intmkdir

(const

char

*pathname, mode_t mode)

;//功能:建立乙個目錄

//返回值:返回0 表示成功, 返回 -1表示錯誤,並且會設定errno值。

//引數:

路徑許可權,注意建立目錄時必須有可執行許可權,否則無法進入目錄

//標頭檔案

#include

//原型

intrmdir

(const

char

*pathname)

;//功能:刪除乙個目錄

//返回值:返回0 表示成功, 返回 -1表示錯誤,並且會設定errno值。

//引數:

路徑

//標頭檔案

#include

#include

//原型

dir *

opendir

(const

char

*name)

; dir *

fdopendir

(int fd)

;//功能: 開啟乙個目錄

//返回值:在失敗的時候返回乙個空的指標,成功時返回dir*型別的指標指向開啟的目錄。

//引數:

//path 路徑

//標頭檔案

#include

//原型

struct dirent *

readdir

(dir *dirp)

;//功能: 讀取開啟的目錄

/*返回值:

struct dirent ;

d_type型別包括:

dt_blk -- 塊裝置

dt_chr --字元裝置

dt_dir -- 目錄

dt_fifo -- 軟連線

dt_lnk -- 管道

dt_reg -- 普通檔案

dt_sock --套接字

dt_unknown --未知

*///引數:

//dirp 開啟的目錄指標

//標頭檔案

#include

#include

//原型

intclosedir

(dir *dirp)

;//功能: 關閉乙個目錄

//返回值:返回0 表示成功, 返回 -1表示錯誤。

//引數:

///dirp 開啟的目錄指標

舉例:遞迴讀取目錄獲取檔案個數

#include

#include

#include

#include

intgetfilenum

(char

* root)

// 讀目錄

int total =0;

char path[

1024]=

;struct dirent* ptr =

null

;while

((ptr =

readdir

(dir))!=

null

)// 判斷是不是檔案

if(ptr-

>d_type == dt_reg)

// 如果是目錄

if(ptr-

>d_type == dt_dir)

}closedir

(dir)

;return total;

}int

main

(int argc,

char

* ar**)

PHP之目錄操作相關函式解釋

目錄相關操作函式 之前我們知道article下有a.txt b.txt c.txt d.txt 我們才去寫 但是當檔案下有e.html f.我們只要處理txt檔案 使用glob 匹配檔案 print r glob txt glob 查詢的是當前目錄下的匹配的 print r glob article...

linux 檔案和目錄操作的相關函式

struct stat 1,stat函式取得檔案資訊。include include int stat const char pathname,struct stat buf int fstat int fd,struct stat buf int lstat const char pathname...

php目錄操作函式

1 mkdir 函式23 php建立資料夾和檔案4 建立資料夾 5function createdir dir 6 9else12 1314 file exists 函式 1516 file exists 檢查檔案或目錄是否存在 17 php18 filename path to foo.txt 1...