以星号显示密码程序!!!!!

来源:互联网 发布:c语言国家二级考试时间 编辑:程序博客网 时间:2024/05/16 12:03
/****************************************************
Function: d_password()
Description: 以星号形式输入密码!
Calls: 
Calls By:
Table Accessed:
Table Updates:
Input:
Output:
Return:
Others: 
****************************************************/
void d_password(char *str)
{


        int i;


        char ch;


        system("stty  -icanon");//关闭缓冲区


        system("stty -echo");//关闭回显


        //密码长度最长为20位
        for(i = 0;i < 20;i++)
        {
        
            ch  = getchar();
            
            //遇换行符结束输入
            if(ch == '\n')
            {
                break;
            }


            //遇退格符删除一个字符
            if(ch == 127 )
            {
                 if(i <= 0)
                  {
                     i--;
                  }
                  else
                  {
                     printf("\b");
            
                                    printf(" ");//用空格代替


                                    printf("\b");

             
                                     i = i - 2;
                         }
            }
            else
            {


                str[i] = ch;
                printf("*");//输出*号
            } 


        }


        str[i] = '\0';//字符串结束


        printf("\n");




        system("stty  icanon");//打开缓冲区


        system("stty echo");//打开回显


}






0 0
原创粉丝点击