用文件存放结构体的问题,求解,急!!!
第二次运行的时候就保存不了,出错了....然后那个txt文件变成了几G大.....求救....#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include <conio.h>
#define N 20
typedef struct Administrator
{
char Username[N]; //用户名
char Password[N]; //密码
struct Administrator *next;
} User;
User *head=NULL,*user=NULL;
int Register(); //注册 用户名,密码
User* Login(); //登录
int Operate(); //改密码
void PsdInput(char a[]); //输入密码时显示星号
int Load();
void Save();
int main()
{
int choose1,choose2;
Load();
while(1)
{
printf("1,注册\n2,登录\n0,退出\n");
scanf("%d",&choose1);
switch(choose1)
{
case 0:return 0;break;
case 1:while(!Register())
{
Register();
}
Save();
printf("\n注册成功!\n\n");
break;
case 2:while(!(user=Login()))
{
user=Login();
}
printf("\n登陆成功,请问你要: \n");
while(choose2!=2)
{
printf("1,修改密码\n2,退出登陆\n");
scanf("%d",&choose2);
switch(choose2)
{
case 1:Operate();Save();printf("\n修改成功\n\n");break;
case 2:break;
default:printf("暂时没有该功能!\n");break;
}
}
break;
default:printf("暂时没有该功能!\n");break;
}
}
}
int Register()
{
char username[N],password[N],temp[N];
User *pNewAdmin,*h=head;
int regOK;
while(1)
{
regOK=1;
printf("请输入用户名: \n");
scanf("%s",username);
h=head;
while(h!=NULL)
{
if(strcmp(h->Username,username)==0)
{
regOK=0;
break;
}
h=h->next;
}
if(regOK==1)
{
printf("请输入密码(最多18位): \n");
PsdInput(password);
printf("\n请确认密码: \n");
PsdInput(temp);
while(strcmp(password,temp)!=0)
{
printf("\n两次输入的密码不同!请重新确认: \n");
PsdInput(temp);
}
pNewAdmin=(User *)malloc(sizeof(User));
strcpy(pNewAdmin->Username,username);
strcpy(pNewAdmin->Password,password);
pNewAdmin->next=head;
head=pNewAdmin;
break;
}
else
{
printf("已经存在相同用户名!\n");
return 0;
}
}
return 1;
}
User* Login()
{
User *h;
char username[N];
char password[N];
int findOK;
while(1)
{
h=head;
findOK=0;
printf("请输入用户名: \n");
scanf("%s",username);
while(h!=NULL)
{
if(strcmp(h->Username,username)==0)
{
findOK=1;
break;
}
h=h->next;
}
if(findOK==1)
{
printf("请输入密码: \n");
PsdInput(password);
while(strcmp(h->Password,password)!=0)
{
printf("\n密码错误!请重新输入: \n");
PsdInput(password);
}
return h; //break;
}
else
{
printf("用户不存在!!请确认用户名!!\n");
return NULL;
}
}
}
int Operate()
{
char password[N],temp[N];
printf("请输入旧的密码:\n");
PsdInput(password);
while(strcmp(password,user->Password)!=0)
{
printf("\n密码输入错误!请重新输入: \n");
PsdInput(password);
}
printf("\n请输入新密码(最多18位): \n");
PsdInput(password);
printf("\n请确认密码: \n");
PsdInput(temp);
while(strcmp(temp,password)!=0)
{
printf("\n两次输入的密码不同!请重新确认: \n");
PsdInput(temp);
}
strcpy(user->Password,password);
return 1;
}
void PsdInput(char a[])
{
char ch;
char psd[N],*p=psd;
int count=0;
ch=getch();
while(ch!=13&&count<N-1)
{
if(ch==8)
{
if(count)
{
p--;
count--;
printf("\b ");
printf("\b");
}
}
else
{
printf("*");
count++;
*p=ch;
p++;
}
ch=getch();
}
psd[--count]='\0';
strcpy(a,psd);
}
int Load()
{
FILE *fp;
User *p1,*p2;
int i;
if(!(fp=fopen("data.txt","rb")))
{
printf("你是第一位使用本系统的用户,请先注册\n\n\n");
return 0;
}
printf("正在载入用户信息..\n\n\n");
head=NULL;
while(1)
{
p1=(User*)malloc(sizeof(User));
p1->next=0;
i=fread(p1,sizeof(User),1,fp);
if(i)
{
if(head==0)
p2=head=p1;
p2->next=p1;
p2=p2->next;
}
else
{
fclose(fp);
if(head==0)
printf("你是第一位使用本系统的用户,请先注册\n\n\n");
else
printf("载入完毕\n\n\n");
break;
}
}
return 1;
}
void Save()
{
FILE *fp;
User *p1,*p2;
fp=fopen("data.txt","wb");
p1=0;
p1=head;
if(head==0)
{
printf("没有用户信息,无法保存!!\n\n");
}
while(p1)
{
fwrite(p1,sizeof(User),1,fp);
p1=p1->next;
}
fclose(fp);
}