C语言数据保存问题

来源:百度知道 编辑:UC知道 时间:2024/05/07 03:04:27
我用C编程 把生成的数据保存为date.dat存在C盘根目录 我在主函数最后也引用了fclose()函数。可是下次再打开程序读保存的数据就读不出来了。不知道C语言保存的数据和硬盘格式有关系么?我硬盘分区都是NTFS的
我的总代码好长的说,总之就是学生成绩管理系统。下面这个子函数就是储存数据用的。
point=fopen("c:\\data.dat","wb+");
printf("\nplease input the total number of students:");scanf("%d",&n);
printf("Number\t\tName\t\tScore1\t\tScore2\t\tScore3\n");
for(i=0;i<=n-1;i++)
scanf("%d%s%d%d%d",&stud[i].No,stud[i].name,&stud[i].score1,&stud[i].score2,&stud[i].score3);
for(i=0;i<=n-1;i++)
{stud[i].sum=stud[i].score1+stud[i].score2+stud[i].score3;stud[i].average=stud[i].sum/3;stud[i].order=0;}
for(i=0;i<=n-1;i++)
if(fwrite(&stud[i],sizeof(struct student),1,point)!=1)
{printf("\nData( %d ) saved error!\n",i);getch();goto end;}
printf("\n\nData saved.Press Any key to continue...\n");fclose(point);

不知道你是用什么方法打开的?用fopen的rb模式应该可以读的.
PS.写文件那里,可以不用循环,直接用
fwrite(stud, sizeof(struct student), n, point));
如果你是用w模式打开的,那每次会把文件清空.

运行程序后,用记事本打开保存的文件,里面有内容么?

你写入的是二进制。
看到乱码不要担心,用UE什么的可以支持二进制的打开,看里面的数据你大概就能明白。
这样写,用二进制一样的办法也可以读回来的。

如果觉得希望写入字符的形式,那么可以fprintf

看看你是怎么写入的啊,fprint?