C语言输出结果存在乱码...

来源:百度知道 编辑:UC知道 时间:2024/05/29 15:41:40
输出结果的时候存在乱码现象 结果后面多出一个问号..
而且将结果写入文件的时候,也是乱码..
求教..主函数省略了..
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <conio.h>
#define NULL 0
#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);
}

这样不会的啊
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <conio.h>
#define NULL 0
#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\n%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", &p1->code);
if(p1->code == 0) break;
scanf("%f%s\n%c", &p1->mark,&p1->name,&p1->sex);