2

Possible Duplicate:
Enter Password in C

Ask user to enter Password without showing the input characters on console?

Community
  • 1
  • 1
Gaurav
  • 28,447
  • 8
  • 50
  • 80

2 Answers2

2

You need to set the console to "no echo" mode. This depends on your particular OS. Here is an example of doing it for linux: http://www.cplusplus.com/forum/beginner/1988/page4.html#msg14522

Andrey
  • 808
  • 2
  • 6
  • 12
0

See the GNU libc documenation for getpass, which gives a handy example of how to implement this:

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

 ssize_t
 my_getpass (char **lineptr, size_t *n, FILE *stream)
 {
   struct termios old, new;
   int nread;

   /* Turn echoing off and fail if we can't. */
   if (tcgetattr (fileno (stream), &old) != 0)
     return -1;
   new = old;
   new.c_lflag &= ~ECHO;
   if (tcsetattr (fileno (stream), TCSAFLUSH, &new) != 0)
     return -1;

   /* Read the password. */
   nread = getline (lineptr, n, stream);

   /* Restore terminal. */
   (void) tcsetattr (fileno (stream), TCSAFLUSH, &old);

   return nread;
 }
Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200