C语言 创建一个文件,输入与输出结构类型数据有关问题
C语言 创建一个文件,输入与输出结构类型数据问题
#include "stdio.h"
#include "stdlib.h"
#define N 2 /*设置学生人数*/
struct student /*定义学生信息结构*/
{int num;
char name[10];
int age;
float score[8];
};
main()
{FILE *fp;
struct student stu1[N],*p=stu1,stu2[N]; /*定义两个结构型数组用于存放学生信息*/
int i,j,max_i;
float x=0.0;
for(;p<p+N;p++) /*输入学生信息存到数组stu1*/
{p->score[7]=0.0;
scanf("%d",p->num);
scanf("%s",p->name);
scanf("%d",p->age);
for(j=0;j<7;j++) /*循环输入每个学生的成绩*/
{scanf("%f",&x);
p->score[j]=x;
p->score[7]+=p->score[j]; /*自动计算学生7科成绩的总分*/
}
}
if((fp=fopen("d:\\delete\\student.dat","wb"))==NULL) /*创建文件student.dat*/
{printf("file can not find!\n");
exit(0);
}
fwrite(stu1,sizeof(struct student),N,fp); /*将stu1中的学生信息存入文件student.dat中*/
fclose(fp); /*关闭文件student*/
if((fp=fopen("d:\\delete\\student.dat","rb"))==NULL) /*重新打开文件student.dat*/
{printf("file can not find!\n");
exit(0);
}
fread(stu2,sizeof(struct student),N,fp); /*读取文件中的学生信息存入stu2*/
fclose(fp);
printf("num name age score1 score2 score3 score4 score5 score6 score7 sum\n"); /*显示输出格式*/
max_i=0;
for(i=0;i<N;i++) /*输出所有学生的信息*/
{printf("%d %s %d %f %f %f %f %f %f %f %f\n",stu2[i].num,stu2[i].name,stu2[i].age,stu2[i].score[0],stu2[i].score[1],stu2[i].score[2],stu2[i].score[3],stu2[i].score[4],stu2[i].score[5],stu2[i].score[6],stu2[i].score[7]);
if(stu2[i].score[7]>stu2[max_i].score[7]) max_i=i; /*自动找出所有学生中总分最高的学生的数组下标*/
}
printf("\n\n\n");
printf("max=\n"); /*以下是输出总分最高的学生的所有信息*/
printf("%d %s %d %f %f %f %f %f %f %f %f\n",stu2[max_i].num,stu2[max_i].name,stu2[max_i].age,stu2[max_i].score[0],stu2[max_i].score[1],stu2[max_i].score[2],stu2[max_i].score[3],stu2[max_i].score[4],stu2[max_i].score[5],stu2[max_i].score[6],stu2[max_i].score[7]);
}
这个程序在turbo for windows2.0中没报错能运行,但是它一直让你输入,就是不出结果。本来我设计的只要输入20个变量就行了,它没完没了的让我输入,好像是一个死循环。晕。
问题出在那里了啊,望解惑。
------解决方案--------------------
for(;p<p+N;p++) /*输入学生信息存到数组stu1*/
这里永远也退出不了啊.
1 < 1+2这是恒成立的吧.
#include "stdio.h"
#include "stdlib.h"
#define N 2 /*设置学生人数*/
struct student /*定义学生信息结构*/
{int num;
char name[10];
int age;
float score[8];
};
main()
{FILE *fp;
struct student stu1[N],*p=stu1,stu2[N]; /*定义两个结构型数组用于存放学生信息*/
int i,j,max_i;
float x=0.0;
for(;p<p+N;p++) /*输入学生信息存到数组stu1*/
{p->score[7]=0.0;
scanf("%d",p->num);
scanf("%s",p->name);
scanf("%d",p->age);
for(j=0;j<7;j++) /*循环输入每个学生的成绩*/
{scanf("%f",&x);
p->score[j]=x;
p->score[7]+=p->score[j]; /*自动计算学生7科成绩的总分*/
}
}
if((fp=fopen("d:\\delete\\student.dat","wb"))==NULL) /*创建文件student.dat*/
{printf("file can not find!\n");
exit(0);
}
fwrite(stu1,sizeof(struct student),N,fp); /*将stu1中的学生信息存入文件student.dat中*/
fclose(fp); /*关闭文件student*/
if((fp=fopen("d:\\delete\\student.dat","rb"))==NULL) /*重新打开文件student.dat*/
{printf("file can not find!\n");
exit(0);
}
fread(stu2,sizeof(struct student),N,fp); /*读取文件中的学生信息存入stu2*/
fclose(fp);
printf("num name age score1 score2 score3 score4 score5 score6 score7 sum\n"); /*显示输出格式*/
max_i=0;
for(i=0;i<N;i++) /*输出所有学生的信息*/
{printf("%d %s %d %f %f %f %f %f %f %f %f\n",stu2[i].num,stu2[i].name,stu2[i].age,stu2[i].score[0],stu2[i].score[1],stu2[i].score[2],stu2[i].score[3],stu2[i].score[4],stu2[i].score[5],stu2[i].score[6],stu2[i].score[7]);
if(stu2[i].score[7]>stu2[max_i].score[7]) max_i=i; /*自动找出所有学生中总分最高的学生的数组下标*/
}
printf("\n\n\n");
printf("max=\n"); /*以下是输出总分最高的学生的所有信息*/
printf("%d %s %d %f %f %f %f %f %f %f %f\n",stu2[max_i].num,stu2[max_i].name,stu2[max_i].age,stu2[max_i].score[0],stu2[max_i].score[1],stu2[max_i].score[2],stu2[max_i].score[3],stu2[max_i].score[4],stu2[max_i].score[5],stu2[max_i].score[6],stu2[max_i].score[7]);
}
这个程序在turbo for windows2.0中没报错能运行,但是它一直让你输入,就是不出结果。本来我设计的只要输入20个变量就行了,它没完没了的让我输入,好像是一个死循环。晕。
问题出在那里了啊,望解惑。
------解决方案--------------------
for(;p<p+N;p++) /*输入学生信息存到数组stu1*/
这里永远也退出不了啊.
1 < 1+2这是恒成立的吧.