typedef struct subjects //定义结构体叫作SUB,在后面就可以直接使用
{
int num; //课程编号
char name[30]; //课程名称
char kind[20]; //课程性质
int stime; //总学时
int ttime; //授课学时
int etime; //实验或上机学时
int score; //学分
int term; //开课学期
struct subjects *next;
}SUB;
void* myInsert(SUB *subj) //链表插入操作
{
SUB *ptr,*ptr2;
ptr=subj;
if(head==NULL)
{
head=ptr;
head->next=NULL;
}
else
{
for(ptr2=head;ptr2;ptr2=ptr2->next)
if(ptr2->next==NULL)
{
ptr2->next=subj;
subj->next=NULL;
break;
}
}
return head;
}
void readfile() //阅读文件 { void *myInsert(SUB*); SUB *newSub; //新课程 int num,stime,ttime,etime; int score,term; char c,name[20],kind[10],fname[20]; FILE *fp; //声明fp是指针,用来指向FILE类型的对象,fp是指向文件结构体的指针变 fp=fopen("2.txt","r"); while(!feof(fp)) { newSub=(SUB*)malloc(sizeof(SUB)); fscanf(fp,"%d%s%s%d%d%d%d%d\n",&newSub->num,newSub->name,newSub->kind,&newSub->stime, &newSub->ttime,&newSub->etime,&newSub->score,&newSub->term); myInsert(newSub); } fclose(fp);}