- 论坛徽章:
- 0
|
再问密码是如何实现的?
本来,这个问题是我昨天问的,今天上午我受到curses库中的getche()函数的启发,用curses函数写了一个小程序,基本上实现的我的要求,现在我贴出的写的代码,请各位大侠们指正:
#include <stdio.h>;
#include <curses.h>;
main(int argc,char **argv)
{
WINDOW *scrwin;
char vtmp[10];
char vpass[40];
char vchar;
int i,k,y;
vtmp[0]=0;
initscr(); /* 初始屏幕 */
noecho();
strcpy(vpass,"请输入口令:" ;
scrwin=newwin(0,0,0,0);
box(scrwin,0,0);
refresh();
wrefresh(scrwin);
y=0;
for (; {
mvwaddstr(scrwin,8,2,vpass);
wrefresh(scrwin);
if (y >; 2){
mvwaddstr(scrwin,20,1,"你是非法使用者,请退出!!!!" ;
wrefresh(scrwin);
break;
}
i=0;
k=strlen(vpass)+2;
while (i !=6){
vchar=getch();
if (vchar=='\010'){ /* 处理BACKSPACE键 */
if (i==0) continue;
if (i >; 0){
k=k-1;
wmove(scrwin,8,k);
waddch(scrwin,' ');
wmove(scrwin,8,k);
wrefresh(scrwin);
i--;
continue;
}
}
mvwaddch(scrwin,8,k,'*');
wrefresh(scrwin);
vtmp=vchar;
if (vchar !='\010') i++;
k++;
}
vtmp[6]=0;
if (strcmp(vtmp,"passwd" ==0){
mvwaddstr(scrwin,20,1,"口令正确,登录成功!!!!!" ;
wrefresh(scrwin);
break;
}else{
mvwaddstr(scrwin,20,1,"口令错误,请重新输入!!!!!" ;
wmove(scrwin,8,(strlen(vpass)+2));
waddstr(scrwin," " ;
refresh();
wrefresh(scrwin);
y++;
continue;
}
}
endwin();
sleep(1);
system("clear" ;
return 0;
} |
|