求指点:C语言链表读文件有关的问题!! 着急坐等!!!!!!
我用fwrite写到文件,但是用fread读取的时候第一行总是有一段乱码
对链表的读写不是很懂 也是照着网上自己改的 怎样避免出现乱码?求指点
对链表的读写不是很懂 也是照着网上自己改的 怎样避免出现乱码?求指点

#include <stdio.h>
#include <stdlib.h>
typedef struct StuNode
{
int num;
char name[20];
char sex[4];
int english_score;
int c_score;
struct StuNode *next;
}StuList;
void main()
{
StuList *input();
void read_file();
void write_file(StuList *head);
StuList *head;
head=input();
write_file(head);
read_file();
system("pause");
}
StuList *input()
{
int x;
bool z=true;
StuList *head,*stu;
head = NULL;
while(z)
{
stu = (StuList*)malloc(sizeof(StuList));
scanf("%d%s%s%d%d",&stu->num,&stu->name,&stu->sex,&stu->english_score,&stu->c_score);
stu->next = head;
head=stu;
printf("是否继续输入下一项记录(1、继续 2、退出):");
scanf("%d",&x);
if(x == 1)
{
z=true;
}
else if( x == 2)
{
z=false;
}
}
return head;
}
void write_file(StuList *head)
{
FILE *fp;
if((fp=fopen("student.txt","a")) == NULL)
{
printf("加载失败!");
return;
}
for (head ; head != NULL ; head = head->next)
{
fwrite(head,sizeof(StuList),1,fp);
}
fclose(fp);
}
void read_file()
{
StuList *head,*stu,*r,*t; //改动:添加*t,作为标记
FILE *fp;
if((fp=fopen("student.txt","r"))==NULL)
{
printf("加载失败!");
}
head=r=(StuList*)malloc(sizeof(StuList));
while(!feof(fp))
{
stu=(StuList*)malloc(sizeof(StuList));;
fread(stu,sizeof(StuList),1,fp);
r->next=stu;
t=r;
r=stu;
}
t->next=NULL;//改动,添加*t
fclose(fp);
if(head == NULL)
{
printf("没有数据!");
}
else
{
for( head ;head != NULL;head = head -> next )
printf("学号:%5d 姓名:%5s 性别:%5s 英语成绩:%5d C语言成绩:%d\n",
head->num,head->name,head->sex,head->english_score,head->c_score);
}
}
...全文
相关推荐
C语言学生成绩管理系统(链表,文件)
利用链表,文件打开等操作实现的学生成绩管理系统。 符合多种要求。
数据结构 求链表长度
数据结构,求链表长度功能,使用C语言开发,界面友好。。
C语言链表和文件实现的超市管理系统
该超市管理系统使用C语言开发,用到了链表和文件的操作。该项目共支持三种类型的用户账户,root管理员,普通管理员,普通用户三种,实现了超市商品的进货,出售以及注册账号等的常用功能。
关于c语言链表的学习总结
我这些天在学习数据结构,发现了一些问题。闲话少叙,先上代码(参考教材:数据结构-c语言,耿国华编著,有删改)。
C语言链表详解
比较适合学习C语言的用户,文档中对链表给出了详尽的讲解。。。。


