/*单链表增加结点*/
#include<stdio.h>
#include<stdlib.h>
typedef struct student{
int id;
int age;
int score;
char name[128];
struct student* next;
}STU,*PSTU;
PSTU head=NULL;
int main(int argc, char*argv[])
{
if(argc<2){
printf("no param!\n");
return -1;
}
FILE * fp;
STU stu[10];
int ret;
int i;
PSTU p;
fp = fopen(argv[1],"r");
if(fp ==NULL){
perror("fopen");
return -1;
}
while(1)
{
p = malloc(sizeof(STU));
ret = fscanf(fp,"%d%d%d%s",
&p->id,
&p->age,
&p->score,
p->name);
if(ret<=0)break;
if(head == NULL){
p->next = NULL;
head = p;
}else{
p->next= head;
head = p;
}
}//end while
printf("ID\tage\tscore\tname\n");
p = head;
while(p)
{
printf("%d\t%d\t%d\t%s\n",p->id,p->age,p->score,p->name);
p = p->next;
}
p = malloc(sizeof(STU));
printf("intput the information of new student\n");
printf("ID\tage\tscore\tname\n");
scanf("%d%d%d%s",
&p->id,
&p->age,
&p->score,
p->name);
p->next = head;
head = p;
printf("after add\n");
printf("ID\tage\tscore\tname\n");
p = head;
while(p)
{
printf("%d\t%d\t%d\t%s\n",p->id,p->age,p->score,p->name);
p = p->next;
}
return 0;
}
/*由于需打开头文件进行操作,可将下内容保存为 stu.txt 文件
1001
20
90
zhangsan
1002
20
100
lisi
1003
20
80
wangwu
1004
20
90
maliu
1005
20
70
zhaoqi
执行时用 ./*** stu.txt
*/