C语言写入文件时出现乱码

来源:百度知道 编辑:UC知道 时间:2024/05/30 00:57:35
程序是要将输出的链表保存到某个 文本文件里面
但是保存之后打开文本文件,出来的都是乱码 不过字符数组部分却是正确的。。希望大虾解答。。原代码如下。。
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <conio.h>
#define LEN sizeof (struct student)
struct student
{
long code;
float mark;
char name[20];
char sex;
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *) malloc(LEN);
scanf("%ld %f %s %c",&p1->code,&p1->mark,&p1->name,&p1->sex);
head=NULL;
while(p1->code!=0)
{n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *) malloc(LEN);
scanf("%ld %f %s %c",&p1->code,&p1->mark,&p1->name,&p1->sex);
}
p2->next=NULL;
return(head);
}
void pri

你上面输出都用printf("%ld %.1f %s %c\n",p->code,p->mark,p->name,p->sex); 了为啥输出到文件时用fwrite
整个结构给过去期望能有什么好的格式?
建议还是老老实实用fprintf
另外在写完数据 一般加个fflush比较好 可以让缓冲数据都输到文件没有缺失
然后要有一个fclose关闭文件