📄 modps.c
字号:
#ifndef MODPASSWORD
#define MODPASSWORD
#include "stdio.h"
#include "user.h"
#include "conio.h"
ModifyMyPassword(user m_user)
{
char DataFile[10]="yonghu";
FILE * fp=NULL;
int err=0,recNumber=0,i=0;
char ps[8],next='n';
user *s,*t;
int total=SIZE;/*SIZE 在user.h中定义为100*/
clrscr();
printf("Name:%s\n",m_user.Name);
do /*输入密码*/
{
printf("Old password(6):");
scanf("%s",ps); /* 输入旧密码 */
ps[7]='\0';
if(strcmp(ps,m_user.ps)!=0) /* 输入的旧密码是否正确 */
{
printf("\nOld password Error!\nAgin(y/n):");
fflush(stdin);
err=1; /* 标志位 */
next=getchar();/* 是否继续 */
}
else
{
err=0;
}
}while((next=='y'||next=='Y')&&err==1);
if(err==1) /* 如果输入错误,退出 */
return;
err=0;
do /*输入新密码*/
{
printf("input new password(6characters):");
scanf("%s",m_user.ps);
m_user.ps[7]='\0';
printf("input password agin(6characters):");
scanf("%s",ps);
ps[7]='\0';
if(strcmp(ps,m_user.ps)!=0) /* 两次输入的密码不一致 */
{
printf("\nTwice password is not same! \nAgin(y/n):");
fflush(stdin);
err=1; /* 标志位 */
next=getchar();/* 是否继续 */
}
else
{
err=0;
}
}while((next=='Y'||next=='y')&&err==1);
if(err==1)
{
return;
}
fp=fopen(DataFile,"rb");
if(fp==NULL)
{
printf("Open file %s fail!",DataFile);
perror("Open file fail");
printf("Press any key to return!");
getch();
return;
}
s=(user*)malloc(SIZE*sizeof(user));
/*读取数据到数组*/
while(fread(&s[recNumber],sizeof(user),1,fp)!=(int)NULL)
{
if(strcmp(s[recNumber].Name,m_user.Name)==0) /*修改对应的PS*/
{
strcpy(s[recNumber].ps,m_user.ps);
}
recNumber++;
if(recNumber>=total)
{
total+=INCREAMENT;
t=(user*)realloc(s,total*sizeof(user));
if(t==NULL)
{
printf("alloc memory fail!\n");
printf("press any key to continu...");
free(s);/*释放内存*/
getch();
return;
}
s=t;
t=NULL;
}
}
fclose(fp);/*关闭文件*/
/*将写入后的数据写回文件*/
fp=fopen(DataFile,"wb");
for(i=0;i<recNumber;i++)
{
if(fwrite(&s[i],sizeof(user),1,fp)!=1)
{
printf("\nwrite file %s fail!\n",DataFile);
perror("Write file fail\n");
printf("press any key to return...");
free(s);/*释放内存*/
getch();
return;
}
}
fclose(fp);
printf("Password modify success!\n");
printf("Press any key to continu...");
getch();
free(s);/*释放内存*/
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -