In a password checking program,when the password is enterd it should not be displayed.Which function should be used for that? If scanf() and gets() are used the text entered will be displayed.
This is a discussion on To enter password without displaying it within the C Programming forums, part of the General Programming Boards category; In a password checking program,when the password is enterd it should not be displayed.Which function should be used for that? ...
In a password checking program,when the password is enterd it should not be displayed.Which function should be used for that? If scanf() and gets() are used the text entered will be displayed.
I don't believe there is a way in standard C to do that. You can use a nonstandard function such as getch to do what you want.
Last edited by kermit; 01-07-2009 at 09:09 AM.
- Using The GNU GDB Debugger: Tutorial with examples and exercises.
In Windows, you can turn off echo for any standard input function with SetConsoleMode().
In *nix you can use the "termios" interface to disable echo.Code:#include <stdio.h> #include <windows.h> int main() { HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); DWORD mode = 0; char buff[25]; GetConsoleMode(hStdin, &mode); SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT)); fgets(buff, sizeof(buff), stdin); SetConsoleMode(hStdin, mode); printf("You entered: %s", buff); return 0; }
ggCode:#include <stdio.h> #include <termios.h> #include <unistd.h> int main() { termios oldt; tcgetattr(STDIN_FILENO, &oldt); termios newt = oldt; newt.c_lflag &= ~ECHO; tcsetattr(STDIN_FILENO, TCSANOW, &newt); fgets(buff, sizeof(buff), stdin); tcsetattr(STDIN_FILENO, TCSANOW, &oldt); printf("You entered: %s", buff); return 0; }
This is a "classic" question. Search the forum and/or FAQ