linux 檔案 基本使用

2021-10-04 23:29:00 字數 4231 閱讀 6370

1. 概述:

該demo主要實現linux下檔案的基本操作, linux系統呼叫, 以及標準io呼叫, 相關介面介紹可以參考<2. 測試:

格式化i/o輸出和輸入相關介面介紹

*/#include

/******************************格式化i/o輸出*************************************/

intprintf

(const

char

*restrict format,..

.);int

fprintf

(file *restrict fp,

const

char

*restrict format,..

.);int

dprintf

(int fd,

const

char

*restrict format,..

.);/* 3個函式返回值:若成功,都返回輸出字元數;若輸出出錯,都返回負值 */

intsprintf

(char

*restrict buf,

const

char

*restrict format,..

.);/* 返回值:若成功,返回存入陣列的字元數;若編碼出錯,返回負值 */

intsnprintf

(char

*restrict buf, size_t n,

const

char

*restrict format,..

.);/* 返回值:若緩衝區足夠大,返回將要存入陣列的字元數;若編碼出錯,返回負值 */

/******************************格式化i/o輸入*************************************/

intscanf

(const

char

*restrict format,..

.);int

fscanf

(file *restrict fp,

const

char

*restrict format,..

.);int

sscanf

(const

char

*restrict buf,

const

char

*restrict format,..

.);/* 3個函式返回值:賦值的輸入項數;若輸入出錯或在任一轉換前已到達檔案尾,都返回 eof */

/*

demo_file_open.c

檔案程式設計demo(linux系統呼叫)

基本操作 : 建立, 開啟, 關閉, 讀寫, 檔案偏移量, 檔案同步, 許可權判斷, 檔案刪除

*/#include

#include

#include

#include

#define max_buf 64

#define filename "./test_open.txt"

intmain

(int argc,

char

**ar**)

/* f_ok : 判斷檔案是否存在

x_ok : 判斷對檔案是否可執行許可權

w_ok : 判斷對檔案是否有寫許可權

r_ok : 判斷對檔案是否有讀許可權

*/if(

access

(filename, f_ok | w_ok | r_ok)

<0)

memset

(buf,

0x0,

sizeof

(buf));

memcpy

(buf, filename,

strlen

(filename));

if(write

(fd, buf,

strlen

(buf)

)<0)

/* seek_set : 引數offset即為新的讀寫位置

seek_cur : 以目前的讀寫位置往後增加offset個位移量

seek_end : 將讀寫位置指向檔案尾後再增加offset個位移量

返回值 : 若成功, 返回新的檔案偏移量(可用於計算檔案大小)

*/if(

lseek

(fd,0,

seek_set

)<0)

printf

("buf = ");

while(1

)else

if(ret ==0)

printf

("%s"

, buf);}

printf

("\n");

fsync

(fd)

;close

(fd)

;//sync();

//unlink(filename);

return0;

err:

fsync

(fd)

;close

(fd)

;//sync();

//unlink(filename);

return-1

;}

/*

demo_file_fopen.c.c

檔案程式設計demo(標準io呼叫)

基本操作 : 開啟/建立, 關閉, 二進位制io讀寫, 檔案偏移量, 檔案同步

*/#include

#include

#include

#define max_buf 64

#define filename "./test_fopen.txt"

intmain

(int argc,

char

**ar**)

/* 二進位制io讀寫

*/memset

(buf,

0x0,

sizeof

(buf));

memcpy

(buf, filename,

strlen

(filename));

if(fwrite

(buf,

strlen

(buf),1

, fp)

<0)

/* seek_set : 檔案的開頭

seek_cur : 檔案指標的當前位置

seek_end : 檔案的末尾

*/if(

fseek

(fp,0,

seek_set

)<0)

printf

("buf = ");

while(1

)else

if(ret ==0)

printf

("%s"

, buf);}

printf

("\n");

fflush

(fp)

;fclose

(fp)

;sync()

;return0;

err:

fflush

(fp)

;fclose

(fp)

;sync()

;return-1

;}

#makefile

cc :

= gcc

include =

-i /home/demo/include/

all:

$(cc) demo_file_open.c $(include)

-o demo_file_open -wall -werror

$(cc) demo_file_fopen.c $(include)

-o demo_file_fopen -wall -werror

clean:

rm demo_file_open demo_file_fopen

linux基本使用

通過今天的學習,我學習了linux作業系統的常用命令,通過ssh命令連線伺服器,通過scp命令上傳檔案,通過useradd新增使用者,並且安裝配置了apache,mysql,jdk,最後將jar包部署在伺服器上。通過課後的回顧,我總結了這次學習中的幾個重要知識點 沒有碟符,一切皆檔案,比如目錄也被成...

Linux基本使用

按鍵 操作c c cancel取消當前操作 c l清空螢幕內容 c d退出當前使用者 c a游標移動到行首 c e游標移動到行尾 c u刪除游標到行首的內容 列印環境變數echo path 檢視檔案內容 其他command options parameter 說明 代表可選 command help...

Linux檔案基本操作

linux最優秀的地方之一,在於它的多使用者 多工的開發環境。為了讓各使用者具有較安全的管理機制,檔案的許可權管理是很重要的。linux通常將檔案的訪問方式分為3個類別,分別是owner group other,且具有read write excute等許可權 由於mac是基於unix系統的,加上我...