求助 建立的链表在调试时链表尾的NULL的地址会改变?
调试运行到save函数中fp1 = fopen("e:\\student1.txt","w")时我发现链表尾NULL的地址会自己改变,导致(p1!=NULL)这个条件最后用不了,因为这个问题作业卡好几天了程序代码:
#include<stdlib.h> #include<string.h> struct stulink{ int num; char name[10]; char sex; int score; char rank; int level; stulink *next; }; void save(stulink* head) { stulink *p1 = head; FILE *fp1; fp1 = fopen("e:\\student1.txt","w"); while(p1!=NULL){ fprintf(fp1,"%d %s %c %d %c",p1->num,p1->name,p1->sex,p1->score,p1->rank); p1 = p1->next; } if(p1 == NULL) printf("所有数据保存成功!!!"); fclose(fp1); } stulink *ReadFromFile() { stulink *head,*temp,*end; head = (stulink *)malloc(sizeof(stulink *)); end = head; FILE *fp1; fp1 = fopen("e:\\student.txt","r"); int count = 1; while(!feof(fp1)){ temp = (stulink *)malloc(sizeof(stulink *)); if(temp == NULL)printf("内存分配失败!!!"); fscanf(fp1,"%s %c %d %c\n",temp->name,&temp->sex,&temp->score,&temp->rank); temp->num = count; count++; end->next = temp; end = temp; } end->next = NULL; fclose(fp1); printf("创建成功!!!\n"); save(head); return head; } void main() { ReadFromFile(); getchar(); }