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.
Printable View
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.
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