#include <stdio.h>
#include <stdlib.h>
#define SIZE 3
struct user {
char name[16];
int age;
} vip[SIZE] = {
{"dan", 43},
{"ben", 51},
{"tom", 62}
};
int main()
{
char filename[16] = "d:\\data.bin";
// write
FILE *fp = fopen(filename, "wb");
if (!fp) {
printf("Unable to open file");
return 1;
}
for (int i = 0; i < SIZE; i++) {
fwrite(&vip[i], sizeof(struct user), 1, fp);
}
fseek(fp, 0, SEEK_END);
long file_size = ftell(fp);
long struct_size = sizeof(struct user);
printf("file size = %ld\n", file_size);
printf("struct size = %ld\n", struct_size);
long total_structs = file_size / struct_size;
printf("total structs = %ld\n", total_structs);
fclose(fp);
return 0;
}
/*
run:
file size = 60
struct size = 20
total structs = 3
*/