1.15标准c库写入结构体到文件
代码展示
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
struct Test
{
int a;
char c;
};
int main()
{
FILE *fp;
struct Test data = {100,'a'};
struct Test data2;
fp = fopen("./file1","w+");
int n_write = fwrite(&data,sizeof(struct Test),1,fp);//yong strlen
fseek(fp,0,SEEK_SET);
// ssize_t read(int fd, void *buf, size_t count);
int n_read = fread(&data2,sizeof(struct Test),1,fp);
printf("read %d,%c \n",data2.a,data2.c);
// int close(int fd);
fclose(fp);
return 0;
}