C语言文件读取乱码问题。
这是结构体structstudent{intsutdentID;charname[20];intChineseScore;intMathScore;intEnglishS...
这是结构体
struct student{ int sutdentID; char name[20]; int ChineseScore; int MathScore; int EnglishScore; float AVScroe; //平均成绩};
这是输出函数的代码
int line_number(FILE *a) // 统计文件行数{ int n; char ch; n = 0; while((ch = fgetc(a)) != EOF) { if(ch == '\n') n ++; } return n;}
void output_all() // 输出全部数据{ FILE *fp; int n; int i; struct student *p; if((fp = fopen("score.txt","r")) == NULL) //open the file { printf("打开文件失败\n"); exit(0); } n = line_number(fp); //检测文件行数 p = (struct student *)malloc(n *(sizeof(struct student))); if(p == NULL) printf("内存分配失败\n"); for(i=0;i<n;i++) { //fscanf(fp,"%d",&p[i].sutdentID); if(fscanf(fp,"%d%s%d%d%d%lf",&p[i].sutdentID,p[i].name,&p[i].ChineseScore,&p[i].MathScore,&p[i].EnglishScore,&p[i].AVScroe) == EOF) printf("error"); //p = p+1; printf("%d %s %d %d %d %f\n",p[i].sutdentID,p[i].name,p[i].ChineseScore,p[i].MathScore,p[i].EnglishScore,p[i].AVScroe); } fclose(fp); free(p);
}
文件读取后输出乱码。是什么问题。 展开
struct student{ int sutdentID; char name[20]; int ChineseScore; int MathScore; int EnglishScore; float AVScroe; //平均成绩};
这是输出函数的代码
int line_number(FILE *a) // 统计文件行数{ int n; char ch; n = 0; while((ch = fgetc(a)) != EOF) { if(ch == '\n') n ++; } return n;}
void output_all() // 输出全部数据{ FILE *fp; int n; int i; struct student *p; if((fp = fopen("score.txt","r")) == NULL) //open the file { printf("打开文件失败\n"); exit(0); } n = line_number(fp); //检测文件行数 p = (struct student *)malloc(n *(sizeof(struct student))); if(p == NULL) printf("内存分配失败\n"); for(i=0;i<n;i++) { //fscanf(fp,"%d",&p[i].sutdentID); if(fscanf(fp,"%d%s%d%d%d%lf",&p[i].sutdentID,p[i].name,&p[i].ChineseScore,&p[i].MathScore,&p[i].EnglishScore,&p[i].AVScroe) == EOF) printf("error"); //p = p+1; printf("%d %s %d %d %d %f\n",p[i].sutdentID,p[i].name,p[i].ChineseScore,p[i].MathScore,p[i].EnglishScore,p[i].AVScroe); } fclose(fp); free(p);
}
文件读取后输出乱码。是什么问题。 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询