c链表输出
我想实现从文件当中把信息录入链表里然后打印出来,如何实现,那一不出错了呢?错误是:p1信息录入不到head里
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Course
{
char sno[10];
char cno[10];
char cname[10];
int xuefen;
int biaoji;
struct Course * next;
};
int count[2][2]={{45,0},{45,0}};
void main()
{
FILE*fp;
struct Course *p1,*p2,*head,*p;
if((fp=fopen("sc.txt","r"))==NULL)
{
printf("open file error!\n");
exit(0);
}
p1=head=(struct Course*)malloc(sizeof(struct Course));
p1=head;
fscanf(fp,"%s%s%s%d%d",p1->sno,p1->cno,p1->cname,&p1->xuefen,&p1->biaoji);
while(!feof(fp))
{
printf("1. %s %s %s %d %d \n",p1->sno,p1->cno,p1->cname,p1->xuefen,p1->biaoji);
p1=(struct Course*)malloc(sizeof(struct Course));
fscanf(fp,"%s%s%s%d%d",p1->sno,p1->cno,p1->cname,&p1->xuefen,&p1->biaoji);
}
p1->next =NULL;
if(fclose(fp))
{
printf("close file error!\n");
exit(0);
}
p2=(struct Course*)malloc(sizeof(struct Course));
p2=head;
while(p2!=NULL)
{
printf("2. %s %s %s %d %d\n",p2->sno,p2->cno,p2->cname,p2->xuefen,p2->biaoji);
p2=p2->next;
}
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Course
{
char sno[10];
char cno[10];
char cname[10];
int xuefen;
int biaoji;
struct Course * next;
};
int count[2][2]={{45,0},{45,0}};
void main()
{
FILE*fp;
struct Course *p1,*p2,*head,*p;
if((fp=fopen("sc.txt","r"))==NULL)
{
printf("open file error!\n");
exit(0);
}
p1=head=(struct Course*)malloc(sizeof(struct Course));
p1=head;
fscanf(fp,"%s%s%s%d%d",p1->sno,p1->cno,p1->cname,&p1->xuefen,&p1->biaoji);
while(!feof(fp))
{
printf("1. %s %s %s %d %d \n",p1->sno,p1->cno,p1->cname,p1->xuefen,p1->biaoji);
p1=(struct Course*)malloc(sizeof(struct Course));
fscanf(fp,"%s%s%s%d%d",p1->sno,p1->cno,p1->cname,&p1->xuefen,&p1->biaoji);
}
p1->next =NULL;
if(fclose(fp))
{
printf("close file error!\n");
exit(0);
}
p2=(struct Course*)malloc(sizeof(struct Course));
p2=head;
while(p2!=NULL)
{
printf("2. %s %s %s %d %d\n",p2->sno,p2->cno,p2->cname,p2->xuefen,p2->biaoji);
p2=p2->next;
}
}
...全文
相关推荐
C语言实现单链表逆序与逆序输出实例
主要介绍了C语言实现单链表逆序与逆序输出,是数据结构与算法中比较基础的重要内容,有必要加以牢固掌握,需要的朋友可以参考下
C语言数据结构实现链表逆序并输出
主要介绍了C语言数据结构实现链表逆序并输出的相关资料,需要的朋友可以参考下
C语言链表的逆向输出程序
在输入所需要的数据后,使用链表方式记录 可以实现链表的逆向输出,使用vc++2008测试
数据结构c语言版单链表的创建和输出
(1)键盘输入n,建立长度为n的单链表并输出; (2)键盘输入x。在单链表中查找值为x的结点并删除,最后输出链表。
C语言 基于链表的日历格式解析 及指定格式输出.rar
先读取日历(文本格式),获取各个事件。每读取一个事件,按时间顺序插入链表。根据程序启动时给定的起止时间参数,输出对应的事件。



