void lianbiao(struct student *head) { FILE *fp; struct student *p1, *q1; q1 = head; if ((fp = fopen("duli16.3.dat", "r")) == NULL) { printf("File open error!\n"); return; } while (1) { p1 = (struct student *)malloc(sizeof(struct student)); if((fscanf(fp, "%s %s %lf", p1->num, p1->name, &p1->money)) ==-1){ break; }; // printf("%s\n",p1->num); p1->next = NULL; q1->next = p1; q1 = p1; } q1->next = NULL; fclose(fp); // printf("111\n"); }
时间: 2024-01-19 19:05:04 浏览: 153
LianBiao.rar_lianbiao_结构体_链表 插入
lianbiao函数的作用是从文件"duli16.3.dat"中读取学生信息,存储到链表中。具体实现代码如下:
```c
void lianbiao(struct student *head) {
FILE *fp;
struct student *p1, *q1;
q1 = head;
if ((fp = fopen("duli16.3.dat", "r")) == NULL) {
printf("File open error!\n");
return;
}
while (1) {
p1 = (struct student *)malloc(sizeof(struct student));
if((fscanf(fp, "%s %s %lf", p1->num, p1->name, &p1->money)) ==-1){
break;
};
p1->next = NULL;
q1->next = p1;
q1 = p1;
}
q1->next = NULL;
fclose(fp);
}
```
dayin函数的作用是遍历链表,输出学生信息。具体实现代码如下:
```c
void dayin(struct student *head) {
struct student *p;
p = head->next;
while (p != NULL) {
printf("%s %s %lf\n", p->num, p->name, p->money);
p = p->next;
}
}
```
阅读全文