取消对NULL指针的引用
//职工信息
typedef struct work
{
int id; //工号
char name[8]; //姓名
int wages; //工资
int bonus; //奖金
int deduction; //扣款
int money; //实发工资
struct work* next; //链表
}workers;
//表头
workers* Listhead()
{
workers* head = (workers*)malloc(sizeof(workers));//动态内存申请 指针变成变量
if (head != NULL)//消除vs警告
{
head->next = NULL;//初始化
return head;
}
return 0;
}
//读取
void read(workers* head)
{
workers* p = head;
workers* temp = (workers*)malloc(sizeof(workers));
FILE* fp = fopen("workers.txt", "r");
if (fp == NULL)
{
fp = fopen("workers.txt", "w+");
}
while (fscanf(fp, "%d%s%d%d%d%d",
&temp->id, temp->name, &temp->wages, &temp->bonus, &temp->deduction, &temp->money) != EOF)
{
if (p != NULL)
{
temp->next = NULL;
p->next = temp;
p = temp;
temp = (workers*)malloc(sizeof(workers));
}
}
fclose(fp);
}
这样写,供参考:
#include <stdio.h>
#include <stdlib.h>
//职工信息
typedef struct work
{
int id; //工号
char name[8]; //姓名
int wages; //工资
int bonus; //奖金
int deduction; //扣款
int money; //实发工资
struct work* next; //链表
}workers;
//表头
workers* Listhead()
{
workers* head = (workers*)malloc(sizeof(workers));//动态内存申请 指针变成变量
if (head != NULL)//消除vs警告
{
head->next = NULL;//初始化
return head;
}
return 0;
}
//读取
void read(workers* head)
{
workers* p = NULL;
workers* temp = NULL; //(workers*)malloc(sizeof(workers));
FILE* fp = fopen("workers.txt", "r");
if (fp == NULL)
{
printf("File open fail!\n");
return; //fp = fopen("workers.txt", "w+");
}
for (p = head;p->next;p = p->next);
while (1){
temp = (workers*)malloc(sizeof(workers));
temp->next = NULL;
if (fscanf(fp, "%d %s %d %d %d %d",&temp->id, temp->name,
&temp->wages,&temp->bonus, &temp->deduction, &temp->money) != 6){
free(temp);
break;
}
p->next = temp;
p = temp;
}
fclose(fp);
}
把&temp的&去掉就行