求高人解答,,咋输出的文件时乱码来的呢,,,,
程序代码:#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
#define LEN sizeof(struct student)
struct student
{
int S_ID;
float score;
char name[10];
struct student *link;
};
struct student *creat();
int main()
{
FILE*file;
char p[2];
char name[20];
int i;
struct student *head;
head=creat();
printf("保存请输入P,退出则输入E.");
scanf("%s",p);
if(0==(strcmp(p,"P")))
{
printf("请输入文件名");
scanf("%s",name);
if( !(file = fopen(name, "wt")))
{
printf("打开文件出错,现在退出程序.");
exit(0);
}
while(head->link)
{
fwrite(head,sizeof(struct student ),1,file);
head=head->link;
}
fclose(file);
}
return 0;
}
struct student *creat()
{
printf("学号输入0则停止输入数据.");
struct student *p1,*p2,*head;
int n;
p1=p2=(struct student *)malloc(LEN);
printf("请输入学号:");
scanf("%d",&p1->S_ID);
printf("\n请输入姓名:");
scanf("%s",&p1->name);
printf("\n请输入成绩:");
scanf("%f",&p1->score);
printf("\n");
head=NULL;
n=0;
while(p1->S_ID)
{
n++;
if(n==1)
{
head=p1;
}
else
{
p2->link=p1;
}
p2=p1;
p1=(struct student *)malloc(LEN);
printf("请输入学号:");
scanf("%d",&p1->S_ID);
printf("\n请输入姓名:");
scanf("%s",&p1->name);
printf("\n请输入成绩:");
scanf("%f",&p1->score);
printf("\n");
}
p2->link=NULL;
return head;
}
唉,,,,咋输出的文件打开后就成了乱码的呢,,,,求解~~~~~~~~~







