问题遇到的现象和发生背景
我做了一个基于哈希表的身份证查询系统,运行程序想把输入的信息写入文件中
void Save(IDLnode*& L)
{
FILE* fp = fopen("D:\\IDcardSearchSystem.txt", "w");//打开文件
if (fp == NULL)
{
perror("打开文件失败\n");
system("pause");
system("cls");
return;
}
IDLnode* p = L;
while (p != NULL)
{
fwrite(&p, sizeof(IDLnode),1, fp);
p = p->next;
}
fclose(fp); //关闭文件
printf("保存成功!\n");
}
void Load(IDLnode*& L)
{
FILE* fp = fopen("D:\\IDcardSearchSystem.txt", "r");//打开文件
if (fp == NULL)
{
perror("\n打开文件失败.\n");
return;
}
IDLnode p;
while (fread(&p, sizeof(IDLnode),1, fp))
{
//创建新节点
IDLnode* pNewNode = (IDLnode*)malloc(sizeof(IDLnode));
pNewNode->next = NULL;
//memcpy(pNewNode, &stu, sizeof(IDLnode));
if (pNewNode != NULL)
{
strcpy(pNewNode->ID, p.ID);
strcpy(pNewNode->Name, p.Name);
strcpy(pNewNode->Address, p.Address);
pNewNode->next = L;
L = pNewNode;
}
}
fclose(fp);
printf("\n读取成功!\n");
system("pause");
system("cls");
}
遇到的现象和发生背景,请写出第一个错误信息
但是当我重新运行程序后,读取信息并将读取的信息打印在屏幕上发现是乱码,而非我之前填入的信息,这是为什么啊
用代码块功能插入代码,请勿粘贴截图。 不用代码块回答率下降 50%
以下是部分代码
#define HashTableLength 50 //哈希表长度
#define IdLength 20 //身份证号长度
#define Prime 47 //质数
#define NameLength 30
#define AddressLength 50
//身份证单链表
typedef struct IDLnode
{
char ID[IdLength]; //身份证号
char Name[NameLength]; //姓名
char Address[AddressLength]; //地址
struct IDLnode* next;
}IDLnode;
//身份证号哈希表
typedef struct IDHashTable
{
char HI_ID[IdLength];
char HI_Name[NameLength];
char HI_Address[AddressLength];
int state;
}IDHashTable;
//姓名哈希表
typedef struct NameHashTable
{
char HN_ID[IdLength];
char HN_Name[NameLength];
char HN_Address[AddressLength];
int state;
}NameHashTable;
bool Insert(IDLnode*& L, IDHashTable*& IH, NameHashTable*& NH, const char* id, const char* name, const char* address)
{
//头插添加进链表
IDLnode* p = (IDLnode*)malloc(sizeof(IDLnode));
if (p != NULL)
{
strcpy(p->ID, id);
strcpy(p->Name, name);
strcpy(p->Address, address);
p->next = L;//->next;
L = p;
}
const char temp[1] = { '\0' };
//用哈希函数抽象出关键字放入哈希表中
int namekey = HashName(name); //获取关键字
int idkey = HashID(id);
//放入身份证号哈希表中
while (IH[idkey].state == 1 && strcmp(IH[idkey].HI_ID, temp) != 0) //线性探测再散列法,用state判断是否冲突,当state=1为冲突,向后存放关键字
idkey++;
strcpy(IH[idkey].HI_ID, id);
strcpy(IH[idkey].HI_Name, name);
strcpy(IH[idkey].HI_Address, address);
IH[idkey].state = 1;
//放入姓名哈希表中
while (NH[namekey].state == 1 && strcmp(NH[namekey].HN_ID, temp) != 0)
namekey++;
strcpy(NH[namekey].HN_ID, id);
strcpy(NH[namekey].HN_Name, name);
strcpy(NH[namekey].HN_Address, address);
NH[namekey].state = 1;
return true;
}