#include
<
stdlib.h
>
#include
<
stdio.h
>
#include
<
conio.h
>
#define
N 4
extern
unsigned _floatconvert;
#pragma extref _floatconvert
struct
student
{
int
num;
char
name[
10
];
char
sex;
int
age;
float
grade;
}stu[N]
=
{{
101
,
"
Zhang
"
,
'
M
'
,
19
,
95.6
},
{
102
,
"
Wang
"
,
'
F
'
,
18
,
92.4
},
{
103
,
"
Zhao
"
,
'
M
'
,
18
,
91.5
},
{
104
,
"
Li
"
,
'
F
'
,
17
,
98.7
}};
void
save()
{
FILE
*
fp;
int
i;
if
((fp
=
fopen(
"
d:\\student.dat
"
,
"
wb
"
))
==
NULL)
{
printf(
"
Cannot open this file!\n
"
);
return
;
}
for
(i
=
0
;i
<
N;i
++
)
if
(fwrite(
&
stu[i],
sizeof
(
struct
student),
1
,fp)
!=
1
)
printf(
"
file write error!\n
"
);
fclose(fp);
}
void
read()
{
FILE
*
fp;
int
i;
struct
student stud[N];
if
((fp
=
fopen(
"
d:\\student.dat
"
,
"
rb
"
))
==
NULL)
{
printf(
"
Cannot open the file!\n
"
);
return
;
}
for
(i
=
0
;i
<
N;i
++
)
{
fread(
&
stud[i],
sizeof
(
struct
student),
1
,fp);
printf(
"
%d\t%s\t%c\t%d\t%.1f\n
"
,stud[i].num,stud[i].name,stud[i].sex,stud[i].age,stud[i].grade);
}
fclose(fp);
}
void
main()
{
clrscr();
save();
read();
getch();
}