关于文件的知识,丢失数据,是怎么回事儿
#include"stdio.h"#include"math.h"
#include"string.h"
#include"stdlib.h"
FILE *fp;
struct information
{
long num;
char name[20];
char major[20];
float Math;
float Chinese;
float English;
}stu[30];
void main()
{
int a,i,j=1;
if((fp=fopen("D:\\work\\test.txt","a+"))==NULL)
{
printf("cannot open this file\n");
exit(0);
}
for(i=0; i<30;i++)//把文件中的内容储存到结构体中
{
fread(&stu[i],sizeof(struct information),1,fp);
}
fclose(fp);
printf("1.录入学生信息\n");
printf("2.输出学生信息\n");
scanf("%d",&a);
switch(a)
{
case 1:
for(i=0;i<30;i++)
{
if((fp=fopen("D:\\work\\test.txt","a"))==NULL)
{
printf("cannot open this file\n");
exit(0);
}
printf("--------------------------------\n");
printf("请输入第%d位的学号:",j);
scanf("%ld",&stu[i].num);
printf("--------------------------------\n");
printf("请输入第%d位的姓名:",j);
scanf("%s",stu[i].name);
printf("--------------------------------\n");
printf("请输入第%d位的专业:",j);
scanf("%s",stu[i].major);
printf("--------------------------------\n");
printf("请输入第%d位的数学成绩:",j);
scanf("%f",&stu[i].Math);
printf("--------------------------------\n");
printf("请输入第%d位的语文成绩:",j);
scanf("%f",&stu[i].Chinese);
printf("--------------------------------\n");
printf("请输入第%d位的英语成绩:",j);
scanf("%f",&stu[i].English);
printf("--------------------------------\n");
j++;
fwrite(&stu[i],sizeof(struct information),1,fp);
fclose(fp);
}
break;
case 2:
printf("\tN0.\tname\tmajor\tMath\tChinese\tEnglish\n");
for(i=0,j=1;i<30;i++)
{
printf("-------------------------------------------------------\n");
printf("%d\t",j);
printf("%d",stu[i].num);
printf("\t%s",stu[i].name);
printf("\t%s",stu[i].major);
printf("\t%.1f",stu[i].Math);
printf("\t%.1f",stu[i].Chinese);
printf("\t%.1f",stu[i].English);
printf("\n");
j++;
}
fclose(fp);
break;
}
}
用文件的方法录入信息后,查询信息是从第26个开始信息就丢失了,这是什么问题???