Linux系統程式設計(八)目錄作業系統呼叫

2021-10-12 15:31:07 字數 3224 閱讀 3471

包含標頭檔案

#include

#include

函式原型

int

mkdir

(const

char

*pathname, mode_t mode)

;

引數

返回值

chmod 示例程式

#include

#include

#include

intmain()

return0;

}

編譯執行結果,檔案許可權0777與許可權掩碼umask=0002做與運算 得到0775

rmdir 同上

包含標頭檔案

#include

函式原型

int

rename

(const

char

*oldpath,

const

char

*newpath)

;

引數

返回值

chmod 示例程式

#include

intmain()

return0;

}

編譯執行結果,

包含標頭檔案

#include

函式原型

int

chdir

(const

char

*path)

;

引數

返回值

chmod 示例程式

編譯執行結果,

包含標頭檔案

#include

函式原型

char

*getcwd

(char

*buf, size_t size)

;

引數

返回值

chdir getcwd示例程式

#include

#include

intmain()

// 獲取當前工作目錄

char buf1[

128]

;getcwd

(buf1,

sizeof

(buf1));

printf

("當前工作目錄: %s\n"

, buf1)

;return0;

}

編譯執行時,子程序工作目錄與父程序相同,切換至/home目錄,在程序結束,返回父程序時,仍在當前工作目錄。

包含標頭檔案

#include

#include

函式原型

dir *

opendir

(const

char

*name)

;

引數

返回值包含標頭檔案

#include

函式原型

struct dirent *

readdir

(dir *dirp)

;

引數

struct dirent

;

返回值

包含標頭檔案

#include

#include

函式原型

int

closedir

(dir *dirp)

;

引數

返回值

#include

#include

#include

#include

#include

intgetfilenum

(const

char

* path)

;int

main

(int argc,

char

* ar**)

int num =

getfilenum

(ar**[1]

);printf

("普通檔案個數: %d\n"

, num)

;return0;

}// 用於獲取目錄下所有普通檔案的個數

intgetfilenum

(const

char

* path)

// 記錄普通檔案個數

int total =0;

struct dirent* ptr ;

while

((ptr =

readdir

(dir))!=

null

)// 判斷是否是普通檔案

if(ptr->d_type == dt_dir)

;sprintf

(newpath,

"%s/%s"

, path, dname)

; total +

=getfilenum

(newpath);}

if(ptr->d_type == dt_reg)

}// 關閉目錄

closedir

(dir)

;return total;

}

編譯執行

Linux 系統程式設計

1 i o操作 2 檔案和目錄管理 3 記憶體管理 1 建立匿名記憶體對映 2 對映 dev zero檔案 類unix 作業系統中,dev zero是乙個特殊的檔案,當你讀它的時候,它會提供無限的空字元 null,ascii nul,0x00 其中的乙個典型用法是用它提供的字元流來覆蓋資訊,另乙個常...

Linux系統程式設計

1.linux程序 守護程序 脫離終端的後台程序 2.linux程序 殭屍程序 3.linux 下程序通訊 其中setsockopt server sockfd,sol socket,so reuseaddr,on,sizeof on 因為每乙個連線都由本地位址和遠端位址的組合唯一確定,所以只要遠端...

Linux系統程式設計 檔案IO操作

檔案描述符 在 linux 的世界裡,一切裝置皆檔案。我們可以系統呼叫中 i o 的函式 i input,輸入 o output,輸出 對檔案進行相應的操作 open close write read 等 開啟現存盤案或新建檔案時,系統 核心 會返回乙個檔案描述符,檔案描述符用來指定已開啟的檔案。這...