0

i have ceated a file in C. i have to give the login security. i already gave the user name & password. but when i type the password the letters are visible how to hide the letters

4
Contributors
6
Replies
7
Views
9 Years
Discussion Span
Last Post by iamthwee
0

When you type characters into the console, its going to STDIN.
you're wanting to modify the character value coming from the keyboard.
There are Windows API functions available for this. off the top of my head i can't remember what they are. MSDN would be your best bet for this.

0

This is unportable... but you might be able to use...

http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1045691686&id=1043284392

For other OS etc the following might also interest you...

http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1042856625&id=1043284385

From that the following seems to work in linux...

#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <ctype.h>

int mygetch ( void ) 
{
  int ch;
  struct termios oldt, newt;
  
  tcgetattr ( STDIN_FILENO, &oldt );
  newt = oldt;
  newt.c_lflag &= ~( ICANON | ECHO );
  tcsetattr ( STDIN_FILENO, TCSANOW, &newt );
  ch = getchar();
  tcsetattr ( STDIN_FILENO, TCSANOW, &oldt );
  
  return ch;
} 

int main()
{
  
  int ch;
  char pword[BUFSIZ];
  int i = 0;
  
  puts ("Enter your password");
  fflush(stdout);
  
  while ((ch = mygetch()) != EOF 
          && ch != '\n' 
          && ch != '\r' 
          && i < sizeof(pword) - 1)
  {
    if (ch == '\b' && i > 0) 
    {
      printf("\b \b");
      fflush(stdout);
      i--;
      pword[i] = '\0';
    }
    else if (isalnum(ch))
    {
      putchar('*');
      pword[i++] = (char)ch;
    }
  }

  pword[i] = '\0';
  
  printf ("\nYou entered >%s<", pword);
  
  return 0;
}

Although the backspace don't work, might be a way round that.

0

hmmm it would appear

if (ch == 0x7f && i > 0) 
    {
      printf("\b \b");
      fflush(stdout);
      i--;
      pword[i] = '\0';
    }

Allows the backspace to be recognised but I'm not sure how reliable it is?

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.