按塊方式讀寫檔案

2021-10-19 13:32:05 字數 1881 閱讀 1273

fwrite函式----向檔案中按塊寫入資料

fread函式------向檔案中按塊讀取資料

如果向檔案中寫入的是自定義結構體資訊,那麼開啟記事本是亂碼,不代表寫入不正確

#define _crt_secure_no_warnings

#include

#include

#include

typedef

struct

stu;

//按塊寫入檔案

void

test()

stu s[3]

=,,}

;//向檔案中寫入資料

//第一種寫法:

int len =

sizeof

(s)/

sizeof

(stu)

;for

(int i =

0; i < len; i++

)fclose

(fp);}

//按塊讀取檔案

void

test01()

stu per[3]

;int len =

sizeof

(per)

/sizeof

(stu)

;//從檔案中讀取資料

//第一種寫法:

for(

int i =

0; i < len; i++

)//列印結構體

for(

int i =

0; i < len; i++)}

intmain()

第二種寫法:結構體陣列儲存時連續位址儲存,所以可以填陣列首位址

#define _crt_secure_no_warnings

#include

#include

#include

typedef

struct

stu;

//按塊寫入檔案

void

test()

stu s[3]

=,,}

;//向檔案中寫入資料

//第二種寫法:

int len =

sizeof

(s)/

sizeof

(stu)

;fwrite

(s,sizeof

(stu),3

, fp)

;fclose

(fp);}

//按塊讀取檔案

void

test01()

stu per[3]

;int len =

sizeof

(per)

/sizeof

(stu)

;//從檔案中讀取資料

fread

(per,

sizeof

(stu),3

, fp)

;//列印結構體

Linux下按扇區讀寫塊裝置

本文介紹linux下按扇區讀寫塊裝置 示例tf卡 實際應用是在android系統上,主要方法如下 1 找到sdcard的掛載點,在android2.1系統下應該為 dev block mmcblk0p1,或是 dev block mmcblk0,而不是 sdcard或 mnt sdcard 2 修改...

python 檔案按行讀寫

1 w 寫模式,它是不能讀的,如果用w模式開啟乙個已經存在的檔案,會清空以前的檔案內容,重新寫 w 是讀寫內容,只要沾上w,肯定會清空原來的檔案 2 r 讀模式,只能讀,不能寫,而且檔案必須存在 r 是讀寫模式,只要沾上r,檔案必須存在 3 a 追加模式,也能寫,在檔案的末尾新增內容 4 rb wb...

json 按行讀寫檔案

在進行大量資料讀寫時,一般json不能讀寫特別大的資料,此時需要按行來進行讀寫 按行寫檔案 import json data for item in data with open data.json a encoding utf 8 as f line json.dumps item,ensure ...