檔案的建立,開啟與關閉

2021-08-02 18:02:45 字數 2309 閱讀 3998

-int chmod(const char *path,mode_t mode);//mode 代表訪問許可權

-int fchmod(int fildes ,mode_t mode);

字元常量值

對應的八進位制數值

含義s_irusr

00400

所有者可讀取

s_iwusr

00200

所有者可寫入

s_ixusr

00100

所有者可執行

s_irgrp

00040

使用者組可讀取

s_iwgrp

00020

使用者組可寫入

s_ixgrp

00010

使用者組可執行

s_iroth

00004

其他人可讀取

s_iwoth

00002

其他人可寫入

s_ixoth

00001

其他人可執行

- usr--使用者  

- grp--使用者組 (group 小組)

- oth--其他人 (other 其他人)

#include

#include

#include

#include

int main(int argc,char **argv)

mode= atoi(argv[1]) ;//引數一定是當作字元處理的

if(mode > 777 || mode < 0)

mode_u = mode /100 ;

mode_g = mode /10 % 10 ;

mode_o = mode %10 ;

mode = mode_u*8*8 + mode_g*8 +mode_o ;//轉換為八進位制

path = argv[2] ;

if( chmod(path,mode)== -1) //呼叫chmod 這個函式

-int open(const char *pathname, int flags, mode_t mode);//flags 代表開啟檔案的方式

常量值

含義o_rdonly

唯讀方式開啟

o_wronly

只寫方式開啟

o_rdwr

可讀寫方式開啟

常量值含義

o_creat

檔案不存在,則自動新建該檔案。此時才需要用到第三個引數,說明新檔案的訪問許可權

o_excl

檢查檔案是否存在,不存在則新建,存在導致開啟檔案出錯

o_trunc

如果檔案存在,且以可寫的方式開啟時,將檔案清零

寫入的資料會追加到檔案後面

-  int creat(const char *pathname, mode_t mode);
1.creat 顧名思義,不管存在不存在都建立新檔案,若存在則新檔案覆蓋舊檔案

2.creat只能以只寫的方式開啟所建立的檔案

3.無法建立裝置檔案

- int close(int fd); //fd 代表檔案描述符
函式

不同點不同點

perror

perror向stderr輸出結果

perror(「字元」) 先輸出字元,再輸出錯誤原因

strerror

strerror向stdout輸出結果

strerror(errno) 將錯誤**轉換為錯誤原因(即人能看的懂的)

linux下檔案的建立,開啟與關閉

1.open 函式 open 系統呼叫用來開啟或建立乙個檔案 函式原型為 include include include int open const char pathname,int flags int open const char pathname,int flags,mode t mode...

檔案的建立開啟關閉讀寫

檔案輸入輸出函式 creat open close read write lseek等 對於核心而言,所有開啟的檔案都由檔案描述符標識 檔案描述符是乙個非負整數。讀寫檔案時 先呼叫open或creat函式開啟檔案,成功執行時都將返回乙個檔案描述符。在讀寫時將其作為引數傳遞給read或write.檔案...

檔案的建立與刪除,開啟與關閉,檔案操作(C)

c語言來直接上傳檔案 實現http協議get post和檔案上傳功能,使用libcurl介面實現 借助開源的libcurl庫,我們可以容易地實現這個功能。libcurl是乙個免費易用的客戶端url傳輸庫,主要功能是用不同的協議連線和溝通不同的伺服器,libcurl當前支援dict,file,ftp,...