C语言的fread()读文件出错

penyatree 2013-04-22 04:06:16
/*
本程序是从一个文件中读取数据,包含学生的编号,姓名,各门成绩,平均成绩,源文件中数已经是按升序排列,要求实现再插入一个学生的数据且也是升序排列并生成一个新文件
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 5
typedef struct{
float cprogram;
float advmath;
float english;
}SCORE;

typedef struct student{
int number;
char name[15];
SCORE testcore;
float avescore;
}STU;

float average(SCORE * sc){
float ave=((*sc).cprogram + (*sc).advmath + (*sc).english)/3;
return ave;
}
int * search(STU stue[],STU *ps){//寻找插入点
int insr=0;//指示器的作用
int * eye=&insr;
STU * pso=stue;
while(pso){
if(pso->avescore < ps->avescore){
pso++;
insr++;
}
else
break;//ps->avescore is the minimum;
}
//*eye=insr;
//printf("%d\n",*eye);//错误的根源,错在哪里有待考究
return eye;
}
void insert(STU stud[],int * pi,STU traValue,FILE * fpth){//插入
int i;
STU *list[SIZE+1];
// STU trys[SIZE+1];
list[0]=stud;
i=0;
for(i=0;i<SIZE;i++){
list[i]=&stud[i];
}
for(i=0;i<SIZE;i++){
printf("%d %s %.2f %.2f %.2f %.2f\n",list[i]->number,list[i]->name,list[i]->testcore.cprogram,list[i]->testcore.advmath,list[i]->testcore.english,list[i]->avescore);
}
for(i=SIZE-1;i>=*pi;i--){
printf("right\n");
list[i+1]=&stud[i];
//printf("%d %s %.2f %.2f %.2f %.2f\n",list[i]->number,list[i]->name,list[i]->testcore.cprogram,list[i]->testcore.advmath,list[i]->testcore.english,list[i]->avescore);
}
printf("error\n");
list[*pi]=&traValue;
printf("error\n");
for(i=0;i<=SIZE;i++){
printf("%d %s %.2f %.2f %.2f %.2f\n",list[i]->number,list[i]->name,list[i]->testcore.cprogram,list[i]->testcore.advmath,list[i]->testcore.english,list[i]->avescore);
}
for(i=0;i<SIZE+1;i++){
// printf("write\n");
fwrite(list[i],sizeof(STU),1,fpth);
/* printf("%derror%d\n",ferror(fpth),i);
printf("%d\n",fwrite(list[i],sizeof(STU),1,fpth));//测试调用成功否
printf("%d\n",ferror(fpth));//测试fwrite()调用是否出错
printf("%d %s %.2f %.2f %.2f %.2f\n",list[i]->number,list[i]->name,list[i]->testcore.cprogram,list[i]->testcore.advmath,list[i]->testcore.english,list[i]->avescore);
*/ }
/*rewind(fpth);
for(i=0;i<SIZE+1;i++){
fread(&trys[i],sizeof(STU),1,fpth);
printf("%derror%d\n",ferror(fpth),i);
//printf("%d\n",ferror());//测试调用是否出错
printf("%d %s %.2f %.2f %.2f %.2f\n",trys[i].number,trys[i].name,trys[i].testcore.cprogram,trys[i].testcore.advmath,trys[i].testcore.english,trys[i].avescore);
}*/

}
int main(void){
FILE * obfp;
FILE * newfp;
int i=0;
int * p;
int n;
float ave;
STU stue[SIZE],stu;
STU newstue[SIZE+1];
if((obfp=fopen("f:\\求职\\newstu.txt","rb"))==NULL){
printf("open file failed\n");
exit(0);
}
printf("the original data:\n");
for(;i<SIZE;i++){
fread(&stue[i],sizeof(STU),1,obfp);
printf("%d %s %.2f %.2f %.2f %.2f\n",stue[i].number,stue[i].name,stue[i].testcore.cprogram,stue[i].testcore.advmath,stue[i].testcore.english,stue[i].avescore);
}
if((newfp=fopen("f:\\求职\\stunew1.txt","wb"))==NULL){
printf("open file failed\n");
exit(0);
}
printf("now please input a new student's data\n");
scanf("%d%s%f%f%f",&stu.number,stu.name,&stu.testcore.cprogram,&stu.testcore.advmath,&stu.testcore.english);
ave=average(&stu.testcore);
stu.avescore=ave;
p=search(stue,&stu);
n=*p;
printf("%d\n",n);
insert(stue,&n,stu,newfp);
rewind(newfp);
for(i=0;i<SIZE+1;i++){
fread(&newstue[i],sizeof(STU),1,newfp);//这里出问题了,读出来是乱码
printf("%d\n",ferror(newfp));//测试调用是否出错

printf("%d %s %.2f %.2f %.2f %.2f\n",newstue[i].number,newstue[i].name,newstue[i].testcore.cprogram,newstue[i].testcore.advmath,newstue[i].testcore.english,newstue[i].avescore);
}
fclose(obfp);
fclose(newfp);
return 1;

}
经过编译能运行,但最终在主函数中读写入新文件的数据时读到的是乱码,想了好几天都没想出来哪里出问题了
恳请各位高手解惑,小弟感激不尽!!!
...全文
7137 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
penyatree 2013-06-14
  • 打赏
  • 举报
回复
引用 6 楼 lm_whales 的回复:
写方式打开的文件,为何去读????!!!
当时确实忽略了,现在知道了
赵4老师 2013-06-14
  • 打赏
  • 举报
回复
推荐使用WinHex软件查看硬盘或文件或内存中的原始字节内容。
lm_whales 2013-06-14
  • 打赏
  • 举报
回复
写方式打开的文件,为何去读????!!!
赵4老师 2013-04-26
  • 打赏
  • 举报
回复
单步调试和设断点调试是程序员必须掌握的技能之一。 代码功能不是被人看出来的;而是被单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中分析出来的。
penyatree 2013-04-25
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
推荐使用WinHex软件查看硬盘或文件或内存中的原始字节内容。 不要把 fopen("...","...");fscanf,fprintf,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待 和 fopen("...","...b");fread,fwrite,fclose //不作以上替换,……
两段代码的函数调用参数都差不多,但为什么第二段能成功输出来,第一段就不行,请老师指正
penyatree 2013-04-23
  • 打赏
  • 举报
回复
两段代码的函数调用参数都差不多,但为什么第二段能成功输出来,第一段就不行, 恳请各位大牛不吝赐教,小弟感激不尽!
penyatree 2013-04-22
  • 打赏
  • 举报
回复
非常感谢老师的回复,但是我还是不能解决这个问题,我还写了一个程序,只是把原来的数据按平均分来升序排序,结构都差不多,但是能成功输出预期结果,不会出现乱码,我把程序贴出来,烦请老师再帮我看一下,谢谢!! #include<stdio.h> #include<stdlib.h> #define SIZE 5 typedef struct{ float cprogram; float advmath; float english; }SCORE; struct student{ int number; char name[15]; SCORE testcore; float avescore; }stu[SIZE]; void sort(struct student stue[],FILE * fpnew){ int i,j; struct student temp; for(i=0;i<SIZE-1;i++){ for(j=i+1;j<=SIZE-1;j++){ if(stue[i].avescore>stue[j].avescore){ temp=stue[i]; stue[i]=stue[j]; stue[j]=temp; } } } for(i=0;i<SIZE;i++){ fwrite(&stue[i],sizeof(struct student),1,fpnew); printf("%d %s %.2f %.2f %.2f %.2f\n",stue[i].number,stue[i].name,stue[i].testcore.cprogram,stue[i].testcore.advmath,stue[i].testcore.english,stue[i].avescore); } } int main(void){ FILE * fp,*fpnew; int i; struct student stue[SIZE]; if((fp=fopen("f:\\求职\\stu.txt","rb"))==NULL){ printf("open file failed\n"); exit(0); } printf("the original order:\n"); for(i=0;i<SIZE;i++){ fread(&stue[i],sizeof(struct student),1,fp); printf("%d %s %.2f %.2f %.2f %.2f\n",stue[i].number,stue[i].name,stue[i].testcore.cprogram,stue[i].testcore.advmath,stue[i].testcore.english,stue[i].avescore); } if((fpnew=fopen("f:\\求职\\newstu.txt","wb"))==NULL){ printf("open file failed\n"); exit(0); } sort(stue,fpnew); rewind(fpnew);//漏掉导致写入乱码; for(i=0;i<SIZE;i++){ fread(&stue[i],sizeof(struct student),1,fpnew); printf("%d %s %.2f %.2f %.2f %.2f\n",stue[i].number,stue[i].name,stue[i].testcore.cprogram,stue[i].testcore.advmath,stue[i].testcore.english,stue[i].avescore); } fclose(fp); fclose(fpnew); return 1; }
赵4老师 2013-04-22
  • 打赏
  • 举报
回复
推荐使用WinHex软件查看硬盘或文件或内存中的原始字节内容。 不要把 fopen("...","...");fscanf,fprintf,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待 和 fopen("...","...b");fread,fwrite,fclose //不作以上替换,遇到\x1a仍继续读;读写的内容当字节看待 弄混了

3,882

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧