我想更改文本中的密码,就写了这样一个密码更改函数。各种调试都好好的,就是成功进入if语句新密码相等时程序直接就结束了,为什么啊。。
void gotoxy(int x,int y)//光标定位函数
{
COORD p;//定义结构体变量p
HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);//获取当前函数句柄
p.X=x;p.Y=y;//将光标的目标移动位置传递给结构体
SetConsoleCursorPosition(handle,p);//移动光标
}
typedef struct password{
char id[8];
char pass_word[8];
}Password;
void modify_stu(){
int index;int x1,x2;
char a2,a,b,c;char password1[8];char id1[8];
char new1_password[8],new2_password[8];
FILE *fp;
char filename[30] = { "账号密码管理.txt" };
if ((fp = fopen(filename, "r+")) == NULL){
printf("can not open!\n");
getchar();
exit(0);
}
Password *present;
system("cls");
printf("*************************************************************************\n");
printf("*** 欢迎进入密码修改界面 ***\n");
printf("*** 请输入账号___________ ***\n");
printf("*** 请输入密码___________ ***\n");
printf("*** ***\n");
printf("*************************************************************************\n");
gotoxy(20,2);
int i;int x,y,z1,z2;
for(i=0;;i++){
index=0;
x=0;y=0;z1=0;z2=0;
while(y<7){
a2=getch();
if(a2=='\b'){
if(y>0){
id1[y-1]='\0';
y--;
}
printf("\b \b");
continue;
}
if(a2!='\r'){
id1[y++]=a2;
putchar(a2);
}
else
break;
}
id1[y]='\0';
gotoxy(20,3);
while (x<7 ){
a = getch();
if (a == '\b'){
if (x>0){
password1[x - 1] = '\0';
x--;
}
printf("\b \b");//先让光标退回一格然后空格覆盖掉要删除字符,然后退回一格重新输入
continue;
}
if (a != '\r'){
password1[x++] = a;
putchar('*');
}
else
break;
}
password1[x] = '\0';
if (Password_confirm(id1,password1)==1){
index = 1;
break;
}
else{
for(int j=0;j<=x-1;j++)
printf("\b \b");
gotoxy(20,2);
printf("________");
gotoxy(20,4);
printf("密码错误,请输入正确密码\n");
gotoxy(20,2);
}
}
if (index == 1){//success
system("cls");
printf("请输入新密码 ");
x1=0,x2=0;
while (x1<7 ){
b = getch();
if (b == '\b'){
if (x1>0){
new1_password[x1 - 1] = '\0';
x1--;
}
printf("\b \b");//先让光标退回一格然后空格覆盖掉要删除字符,然后退回一格重新输入
continue;
}
if (b != '\r'){
new1_password[x1++] = b;
putchar('*');
}
else
break;
}
new1_password[x1] = '\0';
printf("\n请再次输入新密码 ");
while (x2<7 ){
c = getch();
if (c == '\b'){
if (x2>0){
new2_password[x2 - 1] = '\0';
x2--;
}
printf("\b \b");//先让光标退回一格然后空格覆盖掉要删除字符,然后退回一格重新输入
continue;
}
if (c != '\r'){
new2_password[x2++] = c;
putchar('*');
}
else
break;
}
new2_password[x2] = '\0';
if(strcmp(new1_password,new2_password)==0){
FILE *fp;
char filename[30] = { "账号密码管理.txt" };
fp = fopen(filename, "r+");
while(fread(present,sizeof(Password),1,fp)==1){//每次读入一个结构体长度的数据,返回1则代表读取成功
if(strcmp(present->id ,id1)==0){//如果Present所指向的结构体名字与文件中字符串配对成功
strcpy(present->pass_word,password1);
printf("%s",present->pass_word);
fseek(fp,-sizeof(Password),SEEK_CUR);//指针移至修改目标前
fwrite(present,sizeof(Password),1,fp);//将结构体中的内容写入文件
fflush(fp);
fclose(fp);
break;//退出循环
}
else
printf("%s\t%s",present->id,id1);
}
}
else{
printf("%s\t%s",new1_password,new2_password);
fclose(fp);
}
}
}
Password_confirm(id1,password1)这个是判断密码账号密码是否正确的函数
其中主体代码是对的,就是判断之后新密码相等成功后程序就结束了,问题就出在if(strcmp(new1_password,new2_password)==0)里面的代码