linux c程式設計 資料夾操作

2022-05-30 21:30:25 字數 2294 閱讀 8545

建立目錄:

用mkdir

函式建立目錄:

mkdir(const char *pathname, mode_t mode)

引數mode

有下列的組合:

s_isuid 04000 檔案的執行時設定使用者id(

set user-id on execution

)位s_isgid 02000 檔案的執行時設定組id(

set group-id on execution

)位s_isvtx 01000 檔案的儲存正文

(粘著位

sticky)

位s_irwxu            00700 檔案所有者具有讀、寫、執行許可權

s_irusr(

s_iread

)00400 

檔案所有者具可讀取許可權

s_iwusr(

s_iwrite

)00200 

檔案所有者具可寫入許可權

s_ixusr(

s_iexec

)00100 

檔案所有者具可執行許可權

s_irwxg 00070 使用者組具有讀、寫、執行許可權

s_irgrp 00040 使用者組具可讀取許可權

s_iwgrp 00020 使用者組具可寫入許可權

s_ixgrp 00010 使用者組具可執行許可權

s_irwxo 00007 其他使用者具有讀、寫、執行許可權

s_iroth 00004 其他使用者具可讀取許可權

s_iwoth 00002 其他使用者具可寫入許可權

s_ixoth 00001 其他使用者具可執行許可權

**如下

#include 

#include 

void make_dir_function();

struct dirent *readdir(dir *dp); 讀取opendir 返回值的那個列表. 返回dirent結構提指標,dirent結構體成員如下:

struct dirent 

**如下:

void read_files_in_dir()

closedir(dirptr);

}執行結果:

the file name is function.o

the file name is chapter3.c

the file name is main.c

the file name is file.hole

the file name is chapter3

the file name is file_function

the file name is file_function.c

the file name is main.o

the file name is offer_test.c

the file name is .

the file name is function.c

the file name is ..

the file name is main

the file name is offer_test

目錄修改:

每個程序都有乙個當前工作目錄,程序呼叫chdir

或fchdir

函式可以更改當前工作目錄

下面這段**修改工作目錄為/home/zhf/c_prj

void change_dir_function()

但是當我們在shell中用pwd去查詢的時候發現工作目錄還是/home/zhf,修改工作目錄並沒有生效

root@zhf-maple:/home/zhf# pwd

/home/zhf

這是因為每個程式執行在獨立的程序中,shell的當前工作目錄並不會隨著程式呼叫chdir而改變。所以為了改變shell程序自己的工作目錄,shell應當直接呼叫chdir函式。那麼在**中修改了工作目錄如何檢視呢? 這就需要用到getcwd函式

char *getcwd(char *buf,size_t size)  若成功,返回buf, 若出錯,返回null

**如下

void change_dir_function(){

char *ptr;

size_t size;

chdir("/home/zhf/c_prj");

ptr=(char *)malloc(sizeof(size));

getcwd(ptr,size);

printf("the work directory is %s\n", ptr);

the work directory is /home/zhf/c_pr

Linux C 刪除資料夾

方法一 include include include include include include define maxdir 256 char dirname maxdir rootdir maxdir void del dir char path while dirp readdir dp ...

Linux C遍歷資料夾

0x01 首先講一講遍歷資料夾必須要知道的乙個結構體struct dirent struct dirent0x2 具體用法見如下 query.c include include intmain int argc,char ar pdir opendir ar 1 if null pdir while...

資料夾拷貝(linux c 遞迴)

int cpdir char fromdir,char todir mkdir todir,0755 while ptr readdir dir null ret strcmp ptr d name,if 0 ret snprintf fbuf,256,s s fromdir,ptr d name ...