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

Recommended Answers

All 6 Replies

Member Avatar for iamthwee

What operating system are you on?

What operating system are you on?

windows xp

There is no standard way to do that

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.

Member Avatar for iamthwee

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.

Member Avatar for iamthwee

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.