物聯網學習教程—檔案的讀寫二

千鋒教育官方發表於2019-08-27

二、資料塊讀寫函式(fread() fwrite())

函式呼叫:

fread (buffer,size,count fp);

fwrite(buffer,size,count,fp);

引數說明:

buffer :是一個指標。

fread 來說,它是讀入資料的存放地址。

fwrite 來說,是要輸出資料的地址(均指起始地址)。

size :  要讀寫的位元組數。

count : 要進行讀寫多少個 size 位元組的資料項。

fp :    檔案型指標。

 

使用舉例:

 

   若檔案以二進位制形式開啟:

  fread(f,4,2,fp);

   此函式從fp 所指向的檔案中讀入 2 4 個位元組的數 據,儲存到陣列 f 中。

使用舉例:

若有如下結構型別:

struct student_type

{char name[10];

 int num;

 int age;

 char addr[30];}stud[40];

 

可以用fread fwrite 來進行資料的操作:

for (i=0;i<40;i++)

 fread(&stud i ], sizeof(struct student-type) 1 fp); 

for (i=0;i<40,i++)

fwrite(&stud i ], sizeof(struct student-type) 1 fp);

使用舉例:

例1、 從鍵盤輸入4個學生的有關資料,然後把它們轉存 到磁碟檔案上去。

 

#include <stdio.h>

#define SIZE 4

struct student_type

{ char name[10];

      int num;

      int age;

      char addr[15];

}stud[SIZE]; /* 定義結構 */

 void save( )

{FILE *fp;

 int i;

 if((fp=fopen("stu-list","wb"))==NULL)

{ printf("cannot open file\n");

  return;}

 for(i=0;i<SIZE;i++)/* 二進位制寫 */

 if(fwrite(&stud[i],sizeof(struct student_type),1,fp)!=1)

 printf( file write error\n );/* 出錯處理 */

 fclose(fp); } /* 關閉檔案 */   

main()

{int i;

 for(i=0;i<SIZE;i++)/* 從鍵盤讀入學生資訊 */

scanf("%s%d%d%s",stud[i].name,&stud[i].num,

                &stud[i].age,stud[i].addr);

save( );}/* 呼叫 save ()儲存學生資訊 */

 

執行情況如下:

輸入4個學生的姓名、學號、年齡和地址:

       Z hang 1001 19 room - 101

       F un      1002  20  room - 102

       T an      1003  21  room - 103

       L ing    1004  21  room - 104 

 

 #include <stdio.h>

 #define SIZE 4

 struct student_type

{ char name[10];

      int num;

      int age;

      char addr[15];

}stud[SIZE];     

 main( )

{ int i;

FILE*fp;

fp=fopen("stu-list","rb");

for(i=0;i<SIZE;i++)

      {fread(&stud[i],sizeof(struct student_type),1,fp);

       printf("%\-10s %4d      %4d   %\-15s\n",stud[i].name,

         stud[i].num,stud[i]. age,stud[i].addr); }

      fclose (fp);}

 

螢幕上顯示出以下資訊:

Z hang 1001      19    room - 101

F un     1002      20    room - 102

T an     1003      21    room - 103

L ing   1004      21    room - 104 

 #include <stdio.h>

 #define SIZE 4

 struct student_type

{ char name[10];

      int num;

      int age;

      char addr[15];

}stud[SIZE];     

 main( )

{ int i;

FILE*fp;

fp=fopen("stu-list","rb");

for(i=0;i<SIZE;i++)

      {fread(&stud[i],sizeof(struct student_type),1,fp);

       printf("%\-10s %4d      %4d   %\-15s\n",stud[i].name,

         stud[i].num,stud[i]. age,stud[i].addr); }

      fclose (fp);}

 

螢幕上顯示出以下資訊:

Z hang 1001      19    room - 101

F un     1002      20    room - 102

T an     1003      21    room - 103

L ing   1004      21    room - 104 

 

如果已有的資料已經以二進位制形式儲存在一個磁碟檔案

“stu - dat”中,要求從其中讀入資料並輸出到

“stu - list”檔案中,可以編寫一個load函式, 從磁碟檔案中讀二進位制資料。

 

void load( )

 {FILE *fp;int i;

  if((fp=fopen("stu-dat","rb"))==NULL)

    { printf("cannot open infile\n");

      return;}

  for(i=0;i<SIZE;i++)

    if(fread(&stud[i],sizeof(struct student_type),1,fp)!=1)

      {if(feof(fp)) {fclose(fp); return;}

  printf("file read error\n");}

  fclose (fp) }

 


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69914734/viewspace-2655145/,如需轉載,請註明出處,否則將追究法律責任。

相關文章