请高手解答一下,为什么我打印出来的,总是两个相同的数据
为什我输入一个数据后,再打印,出现两个一模一样的数所,比如,我输入,学号:1001 姓名 aaa,打印出为居然是两个1001 aaa,1001 aaa这是为什么,我看讲课上老师打出来的是一个啊,同样是保存再加载。代码如下:#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
void SaveData();
struct MyStud
{
int iNum;
char cName[10];
};
typedef MyStud Data;
struct Node
{
Data data;
Node *pNext;
};
Node *pHead = NULL;
void Addtail(Data data)
{
Node *pNew;
pNew = (Node*)malloc(sizeof(Node));
pNew->data = data;
if (!pHead)
{
pHead = pNew;
pNew->pNext = NULL;
return;
}
Node *p = pHead;
while (p->pNext)
{
p = p->pNext;
}
p->pNext = pNew;
pNew->pNext = NULL;
}
void disPlay()
{
Node *p=pHead;
if(!p)
{
puts("该系统无数据,请添加学生信息");
return;
}
while (p)
{
printf("%d%s", p->data.iNum, p->);
p = p->pNext;
}
}
void AddHead(Data data)
{
Node *pNew;
pNew=(Node*)malloc(sizeof(Node));
pNew->data = data;
pNew->pNext = pHead;
pHead = pNew;
}
void InputData()
{
Data data;
puts("请输入学号");
scanf("%d", &data.iNum);
puts("请输入姓名");
scanf("%s", );
AddHead(data);
disPlay();
SaveData();
}
void SaveData()
{
Node *p = pHead;
FILE *pf = fopen("d:\\1.txt", "w");
if (!pf)
{
puts("打开文件失败");
return;
}
while (p)
{
fwrite(p, 1, sizeof(p->data), pf);
p = p->pNext;
}
fclose(pf);
}
void LoadData()
{
Data data;
Node *p = pHead;
FILE *pf = fopen("d:\\1.txt", "r");
if (!pf)
{
puts("打开文件失败");
return;
}
while (fread(&data,1,sizeof(data),pf)== sizeof(data))
{
AddHead(data);
}
fclose(pf);
}
int main()
{
int seleNum;
do
{
puts("1.浏览信息");
puts("2.添加学员信息");
puts("请输入你的选择");
scanf("%d", &seleNum);
switch (seleNum)
{
case 1: LoadData();
disPlay();
break;
case 2:InputData();
break;
case 3: medify();
break;
case 4: DeleteData();
break;
case 0:
default:
puts("你输入有误,请重新输入");
break;
}
} while (seleNum!=0);
return 0;
}