有关链表的文件读取问题,求高手大大们解决一下
这个文件读取我是套用的数组的文件读取,但不知道为啥老是出错,求解,如果不能套用,如何解决链表的文件读取问题呢?求前辈高手们不吝赐教!
奉上代码:
程序代码:
#include<stdio.h> #include<stdlib.h> #include <string.h> #define ERROR 0 #define OK 1 #define OVERFLOW -1 #define Status int #define MAXSIZE 100 char filename[20]; typedef struct { char no[4]; char name[8]; int price; }Student; struct SqList { Student elem; struct SqList *next; }; int menu_filename() { system("cls"); printf("请输入你要操作的文件名:"); scanf("\t%s", filename); } Status InitList(struct SqList *L)//初始化 { L->next = NULL; return OK; } void DisplayList(struct SqList *L)//显示学生信息 { FILE *fp; struct SqList *tmp = L->next; if ((fp = fopen(filename, "r")) == NULL) //以2进制方式读取文件信息// { printf("cannot open the files\n"); return; } while (!feof(fp)) { fread(&L, sizeof(Student), 1, fp); //以2进制方式将文件信息读到链表内存中//一次读取一个// L= L->next; } fclose(fp); while (tmp != NULL) { printf("学号:%s姓名:%s分数:%d\n", tmp->elem.no, tmp->elem.name, tmp->elem.price); tmp = tmp->next; } } Status Searchelem(struct SqList *L, int i)//3查找对应的数据元素 { FILE *fp; struct SqList *tmp = L->next; int j = 1; if ((fp = fopen(filename, "r")) == NULL) //以2进制方式读取文件信息// { printf("cannot open the files\n"); system("pause"); return; } while (!feof(fp)) { fread(&tmp, sizeof(Student), 1, fp); //以2进制方式将文件信息读到链表内存中//一次读取一个// L = L->next; } while (tmp != NULL) { if (i == j) { printf("第%d号:学号: %s姓名: %s分数: %d\n", i, tmp->elem.no, tmp->elem.name, tmp->elem.price); return OK; } tmp = tmp->next; j++; } return ERROR; } Status SearchElemName(struct SqList *L, char e[])//根据姓名查找 { FILE *fp; printf("查找\n"); struct SqList *tmp = L->next; if ((fp = fopen(filename, "r")) == NULL) //以2进制方式读取文件信息// { printf("cannot open the files\n"); system("pause"); return; } while (!feof(fp)) { fread(&L, sizeof(Student), 1, fp); //以2进制方式将文件信息读到链表内存中//一次读取一个// L = L->next; } while (tmp != NULL) { if (!strcmp(tmp->elem.name, e)) { printf("学号为%s,姓名为%s, 分数是%d\n", tmp->elem.no, tmp->elem.name, tmp->elem.price); return OK; } tmp = tmp->next; } return ERROR; } Status InsertList(struct SqList *L, Student e)//4插入一个数据元素 头插 { FILE *fp; struct SqList *tmp = L; struct SqList *new = (struct SqList *)malloc(sizeof(struct SqList)); new->elem = e; new->next = tmp->next; tmp->next = new; if ((fp = fopen(filename, "a")) == NULL) //以2进制形式进行文件尾追加// { printf("cannot open the files\n"); return; //如果文件出现错误 就返回 } while(L->next!=NULL) { if (fwrite(&L, sizeof(Student), 1, fp) != 1) //以2进制形式写入文件中//一次写入一个结点// printf("file write error\n"); L = L->next; }fclose(fp); //关闭文件 } Status DeleteList(struct SqList *L, int i)//5删除一个数据元素 { FILE *fp; struct SqList *tmp = L; struct SqList *del; int j = 0; if ((fp = fopen(filename, "r")) == NULL) //以2进制方式读取文件信息// { printf("cannot open the files\n"); system("pause"); return; } while (!feof(fp)) { fread(&tmp, sizeof(Student), 1, fp); L=L->next; }i = 0; fclose(fp); if (i < 1) return ERROR; while (tmp != NULL) { if (j == i - 1) { del = tmp->next; if (del == NULL) break; tmp->next = del->next; free(del); return OK; } tmp = tmp->next; j++; } return ERROR; } Status ClearList(struct SqList *L)//6清空顺序表 { FILE *fp; if ((fp = fopen(filename, "r")) == NULL) //以2进制方式读取文件信息// { printf("cannot open the files\n"); system("pause"); return; } while (!feof(fp)) { fread(&L, sizeof(Student), 1, fp); L= L->next; }; fclose(fp); L->next = NULL; return OK; } Status DestorySqList(struct SqList *L)//7销毁顺序表 { L->next = NULL; return OK; } int LengthList(struct SqList *L)//10取表的长度 { int j = 0; struct SqList *tmp = L->next; while (tmp != NULL) { j++; tmp = tmp->next; } return j; } int main() { menu_filename(); struct SqList *L = (struct SqList *)malloc(sizeof(struct SqList)); Student e; int number, N; int no; int i, j; char na[20]; printf("\n"); printf(" 欢迎使用学生信息管理系统(c语言任性版)!\n\n"); printf(" ******************************\n"); printf(" ** 1-创建学生信息表 **\n"); printf(" ** 2-查找学生信息表 **\n"); printf(" ** 3-显示学生信息 **\n"); printf(" ** 4-删除信息表 **\n"); printf(" ** 5-插入学生信息 **\n"); printf(" ** 6-显示学生个数 **\n"); printf(" ** 7-根据姓名查找 **\n"); printf(" ** 0-退出程序 **\n"); printf(" *****************************\n"); number = 1; InitList(L); while (number) { printf("请选择功能序号: "); scanf("%d", &no); switch (no) { case 1: printf("创建成绩表,请输入您要输入学生人数:"); scanf("%d", &N); for (j = 0;j < N;j++) { printf("请输入要插入学生的信息:\n 注:格式为 学号 姓名 分数,每个数据之间空格隔开\n\n"); scanf("%s%s%d", e.no, e.name, &e.price); InsertList(L, e); } break; case 2: printf("请输入您要查找的序号: "); scanf("%d", &i); if (Searchelem(L, i)) printf("查找成功!\n"); else printf("查找失败\n"); break; case 3: DisplayList(L); break; case 4: printf("请输入第i个元素: "); scanf("%d", &i); if (DeleteList(L, i)) printf("删除成功\n"); else printf("删除失败\n"); break; case 5: printf("请输入要插入学生的信息:\n 注:格式为 学号 姓名 分数,每个数据之间空格隔开\n"); scanf("%s%s%d", e.no, e.name, &e.price); if (InsertList(L, e)) printf("插入成功"); else printf("插入失败"); break; case 6: printf("一共有%d个学生信息!\n", LengthList(L)); break; case 7: printf("请输入要查找的名字"); scanf("%s", na); number = 0; break; if (SearchElemName(L, na)) printf("查找成功"); else printf("查找失败\n"); break; default: number = 0; break; } } return 0; }