C語言可以用fread函數從文件中讀取一個數據塊,fwrite函數向文件寫一個數據塊。將數據原封不動的寫入到磁盤上。以二進制的形式。
fread(buffer ,size,count,fp);
fwrite(buffer ,size,count,fp);
現在有一個結構體 struct Student{...........} student1;想將它寫入磁盤上保存下來。
有一個data.txt 空文件 。
FILE *p2file;
p2file = fopen("data.txt","wb");
fwrite(&student1,sizeof(student),p2file);fclose(p2file);
-------------------------------------------------------
現在data.txt已經有了一個數據,用fread讀出來。
FILE *p2file;
p2file = fopen("data.txt","rb");
fread(&student1,sizeof(student1),1,p2file);
printf('name is %s',student.name);
