#include<stdio.h>
void binaryWrite();
void binaryRead();
int main()
{
binaryWrite();
binaryRead();
return 0;
}
//二进制读写
void binaryWrite()
{
Student bloom[47] = { {100,22,"bloom" },{1001,19,"逝川"} ,{1002,20,"hello"} ,
{1003,21,"哈哈哈"}, {1004,18,"幻阳"} };
FILE* fp = fopen("No.1.dj", "wb"); //二进制读写要用二进制的方式打开文件
if (!fp)
{
perror("file open failed");
return;
}
//二进制写入
int ret = fwrite(bloom, sizeof(Student), 5, fp);
printf("write %d block\n", ret);
if (ferror(fp))
{
perror("error ");
}
fclose(fp);
}
void binaryRead()
{
Student vip[10];
FILE* fp = fopen("No.1.dj", "rb"); //二进制读写要用二进制的方式打开文件
if (!fp)
{
perror("file open failed");
return;
}
//读取二进制数据
int ret = fread(vip, sizeof(Student), 10, fp);
printf("ret %d \n", ret);
fclose(fp);
}