马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<string.h>
struct student{
int number;
char name[20];
int age;
}stu[4];
void init(){
stu[0].number = 1;
strcpy(stu[0].name,"Jim");
stu[0].age = 20;
stu[1].number = 2;
strcpy(stu[1].name,"Bob");
stu[1].age = 21;
stu[2].number = 3;
strcpy(stu[2].name,"Tom");
stu[2].age = 22;
stu[3].number = 4;
strcpy(stu[3].name,"Mark");
stu[3].age = 23;
}
void save(){
FILE *fp;
int i;
if(!(fp=fopen("student","wb"))){
printf("打开失败...\n");
return ;
}
for(i = 0 ;i < 4; i ++){
fwrite(stu+i,sizeof(struct student),1,fp);
}
fclose(fp);
printf("写入成功...\n");
}
void load(){
FILE *fp;
struct student s[4];
int i;
if(!(fp=fopen("student","rb"))){
printf("打开失败...\n");
return ;
}
printf("********************显示数据开始********************\n");
printf("学号\t\t姓名\t\t年龄\n");
for(i = 0 ; i < 4; i++){
fread(s+i,sizeof(struct student),1,fp);
printf("number:%-2d\tname:%-5s\tage:%-4d\n",s[i].number,s[i].name,s[i].age);
}
printf("********************显示数据结束********************\n");
fclose(fp);
printf("读取完成....\n");
}
int main()
{
system("color f0");
init();
save();
load();
return 0;
}
|