36 lines
625 B
C
36 lines
625 B
C
|
// 自定义二进制文件写入
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
typedef struct
|
||
|
{
|
||
|
int sid;
|
||
|
char name[32];
|
||
|
int age;
|
||
|
char sex[4];
|
||
|
} STU;
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
STU s1 = {1, "disen", 20, "男"};
|
||
|
STU s2 = {2, "jack", 18, "女"};
|
||
|
STU *s3 = malloc(sizeof(STU));
|
||
|
s3->sid = 3;
|
||
|
strcpy(s3->name, "lily");
|
||
|
s3->age = 19;
|
||
|
strcpy(s3->sex, "女");
|
||
|
|
||
|
FILE *f = fopen("d13.txt", "wb");
|
||
|
if (NULL == f)
|
||
|
{
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
fwrite(&s1, sizeof(STU), 1, f);
|
||
|
fwrite(&s2, sizeof(STU), 1, f);
|
||
|
fwrite(&s3, sizeof(STU), 1, f);
|
||
|
fclose(f);
|
||
|
|
||
|
return 0;
|
||
|
}
|