输入学生的学号 姓名 三门课成绩 平均分 然后保存到一个文件上,为什么文件的成绩部分是乱码
#include "stdio.h"#include "stdlib.h"
#include "string.h"
#define LEN sizeof(struct student)
struct student
{
char num[5];
char name[20];
float score[3];
float ave;
struct student*next;
}
main()
{
void creat_fput(struct student*);
struct student *head;
head=(struct student*)malloc(LEN);
creat_fput(head);
}
void creat_fput(struct student*head)
{
struct student *p1,*p2;
FILE*fp;
int i,j=1;
float sum=0;
fp=fopen("stud3.16","wb");
if(fp==NULL)
printf("文件建立失败!\n");
printf("请输入学生信息,以学号为#号结束:\n");
p1=head;
do
{
printf("第%d个学生:\n",j);
printf("学号:");
scanf("%s",p1->num);
if(p1->num[0]=='#')
break;
fwrite(&p1->num,strlen(p1->num),1,fp);
fputc(' ',fp);
printf("姓名:");
scanf("%s",p1->name);
fputc(' ',fp);
fwrite(&p1->name,strlen(p1->name),1,fp);
printf("成绩:\n");
for(i=0;i<3;i++)
{printf("%c:",'A'+(char)i);
scanf("%f",&p1->score[i]);
fwrite(&p1->score[i],4,1,fp);
sum=sum+p1->score[i];}
p1->ave=sum/3;
fwrite(&p1->ave,4,1,fp);
fputc('\n',fp);
p2=p1;
p1=(struct student*)malloc(LEN);
p2->next=p1;
sum=0;
j++;
}while(1);
p2->next=NULL;
fclose(fp);
}
[[italic] 本帖最后由 llp108 于 2008-1-19 21:01 编辑 [/italic]]