文件输入输出//有些急
在文件输入学生姓名学号成绩然后统计不同等级人数,然后读取文件显示这些信息我输入第一个就暂停了,请问是为什么?
#include<stdio.h>
struct m
{
char name;
long int number;
int score;
};
void main()
{
struct m student;
struct m *n = student;
int A,B,C,D,E;
int a = 0,b = 0,c = 0,d = 0,e = 0,num,i;
FILE *p;
if((p = fopen("new.txt","w+")) == NULL)
{
printf("打开文件失败!\n");
exit(0);
}
for(i=0;i<30;i++)//录入信息,统计人数
{
printf("学生姓名,学号,成绩(输入-1停止):");
scanf_s("%s%ld%d",(n+i)->name,&(n+i)->number,&(n+i)->score);
if((n+i)->name == "-1")
{
break;
}
if((n+i)->score<60)
{
e ++;
}
if((n+i)->score>=60&&(n+i)->score<70)
{
d ++;
}
if((n+i)->score>=70&&(n+i)->score<80)
{
c ++;
}
if((n+i)->score>=80&&(n+i)->score<90)
{
b ++;
}
if((n+i)->score>=90&&(n+i)->score<=100)
{
a ++;
}
fwrite(n+i,sizeof(struct m),1,p);
}
fprintf(p,"%d%d%d%d%d",a,b,c,d,e);//输入各等级人数
if(fclose(p))
{
printf("Can not close the file\n");
}
if((p = fopen("new.txt","r")) == NULL)
{
printf("打开文件失败!\n");
exit(0);
}
//输出
num = i;
for(i=0;i<num;i++)
{
printf("\n第%d个同学姓名,学号,成绩:",i+1);
fread(n+i,sizeof(struct m),1,p);
}
fscanf(p,"%d%d%d%d%d",&A,&B,&C,&D,&E);
printf("A:%d\nB:%d\nC:%d\nD:%d\nE:%d\n",A,B,C,D,E);
if(fclose(p))
{
printf("Can not close the file\n");
}
} scanf_s中取地址后面都括起来
页:
[1]