一个只能用在Win下的密码验证函数(显示星号,可删除)

以前做小程序时图好玩在网上找的代码。输入的密码会以星号显示出来,并且输入错了可以删除。因为用了专有库函数,所以只能在Windows平台使用,少用为好,不过可能还有点用。嗯…就这样了

#include <stdio.h>
 
#include <string.h>
 
#include <conio.h>
 
#define  MAX_PASSLEN 128           //定义密码长度
 
char tpass[20]="admin";
 
void GetPassword(char *szFinalPass)
 
{
 
    char chValue,szPassword[MAX_PASSLEN];
 
    int iCounter = 0;                //定义计数器
 
    while ( 1 )
 
    {
 
        if( ( chValue = getch() ) != '\r' )                //如果输入的不是回车
 
        {
 
            if( chValue != '\b' )                                //如果输入的不是退格
 
            {
 
                if ( iCounter < MAX_PASSLEN )         //如果长度并未超过密码的最大长度
 
                {
 
                    szPassword[iCounter] = chValue;
 
                    putchar( '*' );                               //在屏幕上显示星号
 
                    iCounter ++;
 
                }
 
                else
 
                {
 
                    putchar( '\7' );                             //如果密码已经超过最大长度,则响铃报警
 
                }
 
            }
 
            else
 
            {
 
                if( iCounter != 0 )                                  //如果按了退格,并且当前不是第一个字符
 
                {
 
                    iCounter --;
 
                    printf( "\b \b" );                           //注意两个\b之间是有个空格的,含义是先退格,
 
                    //然后打印空白字符将之前的字符覆盖掉,然后再退格使光标退回
 
                }
 
            }
 
        }
 
        else
 
        {
 
            szPassword[iCounter] = 0;                         //密码输入结束时将末尾以\0结尾!
 
            break;
 
        }
 
    }
 
    strcpy( szFinalPass ,szPassword );                              //最终将密码复制出来
 
}
 
int main()
 
{
 
    char szPassword[128];
 
    GetPassword(szPassword);
 
    if(strcmp(szPassword,tpass)==0)
        printf("\n输入的密码是:%s\n",szPassword);
 
    else
        printf("\n密码错误\n");
}

 

posted on 2014-05-03 15:34  卖火柴的小东东  阅读(146)  评论(0编辑  收藏  举报

导航