Hide password input on terminal

后端 未结 15 1369
清酒与你
清酒与你 2020-11-22 09:55

I want to mask my password while writing it with *. I use Linux GCC for this code. I know one solution is to use getch() function like this

15条回答
  •  盖世英雄少女心
    2020-11-22 10:36

    printf("\nENTER PASSWORD: ");
    while (1)
    {
        ch=getch();
        if(ch==13)    //ON ENTER PRESS
            break;
    
        else if(ch==8)    //ON BACKSPACE PRESS REMOVES CHARACTER
        {
            if(i>0)
            {
                i--;
                password[i]='\0';
                printf("\b \b");
            }
        }
        else if (ch==32 || ch==9)    //ON PRESSING TAB OR SPACE KEY
            continue;
        else
        {
            password[i]=ch;
            i++;
            printf("*");
        }         
    }
    password[i]='\0';
    

提交回复
热议问题