標準C庫開啟建立檔案,讀寫檔案,游標移動

2021-10-11 18:22:23 字數 1383 閱讀 9587

**1.file *fopen(const char path, const char mode);

引數說明:

**2.size_t fwrite(const void ptr, size_t size, size_t nmemb, file stream);

引數說明:

size:要寫入內容的單位元組數nmemb:要寫入內容的個數stream:目標檔案指標

返回值:隨著呼叫格式的不同而不同。1)呼叫格式:fwrite(buf,sizeof(buf),1,fp);若成功寫入,返回值為1。2)呼叫格式:fwrite(buf,1,sizeof(buf),fp);若成功寫入則返回實際寫入的資料個數。

**3.size_t fread(void ptr, size_t size, size_t nmemb, file stream);

引數說明:

*4.int fseek(file stream, long offset, int whence);

引數說明:

*5.int fclose(file fp);

作用:關閉乙個檔案流

注意:當開啟乙個檔案時,記得一定要關閉它!

#include

#include

intmain()

; file *fp;

fp =

fopen

("./chen.txt"

,"w+");

fwrite

(str,

sizeof

(char),

strlen

(str)

,fp)

;// 也可以寫成:fwrite(str,sizeof(char)*strlen(str),1,fp);

fseek

(fp,0,

seek_set);

fread

(readbuf,

sizeof

(char),

strlen

(str)

,fp)

;// 也可以寫成:fread(readbuf,sizeof(char)*strlen(str),1,fp);

printf

("read data:%s\n"

,readbuf)

;fclose

(fp)

;return0;

}

C 建立資料夾 讀寫TXT檔案

寫內容應該使用utf8格式,不然讀出來可能會是亂碼 string curtimepath tmppath datetime.now.tostring yyyymmddhhmmss if directory.exists curtimepath string newtxtpath curtimepat...

python讀寫 建立 檔案(一)

python中對檔案 資料夾 檔案操作函式 的操作涉及到os模組和shutil模組。得到當前工作目錄,即當前python指令碼工作的目錄路徑 os.getcwd 返回指定目錄下的所有檔案和目錄名 os.listdir 函式用來刪除乙個檔案 os.remove 刪除多個目錄 os.removedirs...

開啟建立檔案Open Create 函式

linux中 open 用於開啟已經存在的檔案或者建立乙個新檔案 create 用於建立乙個新檔案。int open const char pathname,int flag int open const char pathname,int flag,mode t mode 注 pathname 不...