typedef struct Account
{
char cardNum[17];
char password[7];
char userName[10];
char balance[20];
struct Account* next;
}LIST;
LIST* head=NULL;
LIST* p = NULL;
void initialize() {
FILE* fp;
LIST* tail,*temp;
head = (LIST*)malloc(sizeof(LIST));
tail = head;
p = head;
if ((fp = fopen("D:\\华软2021-2022\\c2\\ATM模拟机\\accounts.txt", "r")) == NULL) {
printf("文件打开错误");
exit(0);
}
while (1) {
temp = (LIST*)malloc(sizeof(LIST));
fscanf(fp, "%s\t%s\t%s\t%s\n", temp->cardNum, temp->password, temp->userName, temp->balance);
temp->next = NULL;
tail->next = temp;
tail = temp;
if (feof(fp)) {
break;
}
}
fclose(fp);
}
//释放空间
void freeSpace() {
LIST* temp = p->next;
while (temp!=NULL) {
p = temp->next;
free(temp);
temp = p;
}
}
供参考:
//释放空间
void freeSpace(LIST* head) {
while (head != NULL) {
LIST* temp = head;
head = head->next;
free(temp);
//temp = p;
}
head = NULL;
}
你这样写没有释放头节点,而且P的赋值在另外一个函数里面,这样不安全。