#include struct student
{
char name[20];
char sex[5];
char num[100];
int age;
int high;
int weight;
}stu;
void input(struct student *p)
{
FILE *fp;
fp=fopen("information.dat","wb");
printf("请输入学生的信息:
");
printf("在姓名处输入-1表示结束
");
while(1)
{
printf("姓名:");
scanf("%s",p->name);
if(!strcmp(p->name,"-1"))
break;
printf("性别:");
scanf("%s",p->sex);
printf("学号:");
scanf("%s",p->num);
printf("年纪:");
scanf("%d",&stu.age);
printf("身高:");
scanf("%d",&stu.high);
printf("体重:");
scanf("%d",&stu.weight);
fwrite(p,sizeof(struct student),1,fp);
}
fclose(fp);
printf("输入完成!");
system("pause");
system("cls");
}
void output1(struct student *p)
{
FILE *fp1,*fp2; char name1[20];
fp1=fopen("information.dat","rb+");
fp2=fopen("temp","wb+");
printf("请输入学生姓名:
");
scanf("%s",name1);
while(1)
{
fread(p,sizeof(struct student),1,fp1);
if(!strcmp(p->name,name1))
{
fwrite(p,sizeof(struct student),1,fp2);
}
break;
}
fclose(fp1);
fclose(fp2);
remove("information.dat");
rename("temp","information.dat");
fp1=fopen("information.dat","rb");
fread(p,sizeof(struct student),1,fp1);
printf("姓名:%s
",p->name);
printf("性别:%s
",p->sex);
printf("学号:%s
",p->num);
printf("年纪:%d
",stu.age);
printf("身高:%d
",stu.high);
printf("体重:%d
",stu.weight);
fclose(fp1);
system("pause");
system("cls");
}
void output2(struct student *p)
{
FILE *fp;
if ((fp=fopen("information.dat","rb")) == NULL)
{
printf ("文件打开失败!
");
exit (0);
}
fread(p,sizeof(struct student),1,fp);
while(!feof(fp))
{
printf("姓名:%s
",p->name);
printf("性别:%s
",p->sex);
printf("学号:%s
",p->num);
printf("年纪:%d
",stu.age);
printf("身高:%d
",stu.high);
printf("体重:%d
",stu.weight);
fread(p,sizeof(struct student),1,fp);
}
fclose(fp); system("pause");
system("cls");
}
void end()
{
exit(0);
}
void main()
{
struct student *a=&stu;
int chose;
while(1)
{
printf("
");
printf("请输入正确的选择:
");
printf("1.全部输入
");
printf("2.按照姓名查找
");
printf("3.全部输出
");
printf("4.退出
");
printf("请输入选择:");
scanf("%d",&chose);
switch(chose)
{
case 1:input(a);
break;
case 2:output1(a);
break;
case 3:output2(a);
break;
case 4:end();
}
}
}