C语言文件一小问!求大神

问题。我写了数据存放到文本,保存后。再次增加数据进去就出现问题。

1.一开始存入两个学生数据。没出错
图片说明
2.当我再往里面添加1个学生信息
图片说明
3.我次运行程序都会先刷新一下,增加数据都会保存。
void shuaxin() //刷新学生信息函数
{
FILE *fp;
int i=0; //记录学生人数
if((fp=fopen("student.txt","r"))==NULL)
{
printf("文件刷新失败\n");
exit(0);
}
while(fscanf(fp,"%s %s %s%f%f%f%f",stu[i].num,stu[i].name,stu[i].time,&stu[i].score[0],&stu[i].score[1],
&stu[i].score[2],&stu[i].sum)==7)
i++;
fclose(fp);
n=i; //n是学生总人数
printf("………………………………………%d位学生信息刷新录入完毕………………………………\n",n);
system("pause");
}
void baocun() //保存学生信息函数
{
FILE *fp;
int i=0; //录入学生人数
if((fp=fopen("student.txt","w"))==NULL)
{
printf("文件保存失败\n");
exit(0);
}
for(i=0;i<n;i++)
{
fprintf(fp,"%s %s %s %.2f %.2f %.2f %.2f",stu[i].num,stu[i].name,stu[i].time,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].sum);
fputs("\n",fp);
}
fclose(fp);
printf("………………………………一共有%d个学生信息已经保存……………………\n",n);
}
出题出现在哪里?

你重发一下代码,用选项里的“插入代码片”括起来,这样代码看不到,就是这个图标:“</>”

if((fp=fopen("student.txt","w"))==NULL)改成if((fp=fopen("student.txt","a"))==NULL)试试

txt写有多种方法,ifstream ofstream 支持在文本最后插入,也比较方便。 http://blog.csdn.net/hong__fang/article/details/43488265