|
[code:1]#include "stdio.h"
struct birthday
{
int year, month, day;
};
struct student
{
long num;
char name[10];
struct birthday birth;
} stu[5], s[5]; //这是对象数组吗?如果不在这声明,该怎么声明。
main()
{
FILE *fp;
int i;
for(i = 0; i < 5; i++)
{
scanf("%ld",&stu[i].num);
scanf("%s",stu[i].name);
scanf("%d,%d,%d",&stu[i].birth.year,&stu[i].birth.month,&stu[i].birth.day);
}
if((fp=fopen("student.dat","wb"))==NULL)
{
printf("file can not open\n");
exit(0);
}
fwrite(stu,sizeof(struct student),5,fp);
fclose(fp);
if((fp=fopen("student.dat","rb"))==NULL)
{
printf("file can not open\n");
exit(0);
}
fread(s,sizeof(struct student),5,fp);
fclose(fp);
for(i = 0;i<5;i++)
{
printf("num: %6ld name: %10s birthday: %3d\n",s[i].num,s[i].name,s[i].birth.year); //%6ld是什么意思?为什么要加个6,我试过,不加也可以,6是起什么作用?
}
}
[/code:1] |
|