C语言如何用fscanf读取以下fwrite函数保存的文件?追加50分。

void save()
{
int i=0;
FILE *pFile=NULL;
char buf[100]={0};
memset(buf,0,100);
if((pFile=fopen("student.dat", "w" ))!= NULL )
{
fwrite(buf,1,sizeof(buf),pFile);memset(buf,0,100);
for(i=0;i<StuDex;i++)
{
if(StuFG[i]>0)
{
sprintf(buf," %s %s %d %f %f %f\n",Student[i].stuId,Student[i].stuName,Student[i].stuSex,Student[i].scoreCh,Student[i].scoreMath,Student[i].scoreEn);
fwrite(buf,1,sizeof(buf),pFile);memset(buf,0,100);
}
}
}
fclose(pFile);
printf("\n数据保存成功!\n");
system("pause");
return;
}

FILE *fp;
int i=0;

fp=open("student.dat","r");
while(fscanf(fp," %s %s %d %f %f %f\n",Student[i].stuId,Student[i].stuName,&Student[i].stuSex,&Student[i].scoreCh,&Student[i].scoreMath,&Student[i].scoreEn)!=EOF)
{
i++;
}
fclose(fp);
仅供参考
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-08-18
你这个程序不能用fscanf()函数读取文件数据,只能用fread()函数来读!追问

听老师说稍微改一下就行了可是我不会改。。用fread()怎么写呢,,跪求啊

追答

你的原代码就有问题,帮你修改了一下,字数超了,再追问,我再显示读文件
你的程序应该是fprintf()函数来保存,再用fscanf()来读取!
void save()
{
int i=0;
FILE *pFile=NULL;
if((pFile=fopen("student.dat", "w" ))!= NULL )
{
for(i=0;i0) //这个if是什么意思?
{
fprintf(pFile," %s %s %d %f %f %f\n", Student[i].stuId,Student[i].stuName,
Student[i].stuSex,Student[i].scoreCh, Student[i].scoreMath,Student[i].scoreEn);
}
}
fclose(pFile);
printf("\n数据保存成功!\n");
}
}

相似回答