Invisible chars.

How do i get the STDIN invisible . Example when i want the user to type in a password  but the chars he type won't be visible on the screen or visible with *******.
marcooAsked:
Who is Participating?
 
tzxie2000Connect With a Mentor Commented:
bool GetPassword(char * lpPassword,int nMaxPasswordLen)
{
  int i;
  char * p;
  char c;
 
  p = lpPassword;
  for(;;)
  {
    c = getch();
    if("\27" == c)//ESC here
      break;
    if("\13" == c)//ENTER here
      break;
    if("\08" == c)//BACKSPACE here
    {
      if(p>lpPassword)
        p--;
      continue;
    }
    if ((p-lpPassword)>=nMaxPasswordLen)
      continue;
    *p = c;
    p++;
  }
  if("\13" == c)//Confirm Enter
  {
    *p == 0;
    return true;
  }
  if ("\27" == c)//Cancel Enter
    return false;
  return false;
}
0
 
inprasCommented:
Hi
If U want it to do for every character enterd through keyboard U have to erite a TSR which checks for the keyboard and replaces every character with "*"

or else if U want to do it for UR perticular App then I think its quite easy replacing the in buffer with "*"s
0
 
imladrisCommented:
No, it is much simpler than that. Use getchar for regular occasions. It echoes the characters to stdout (which is normally the console). To avoid console output use: getch. In this case you can output '*'s to the console explicitly as needed.
0
Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

 
MatthewLCommented:
Marcoo - What's your O/S?

Matt
0
 
marcooAuthor Commented:
Linux is my OS!
0
 
marcooAuthor Commented:
Imladris! can i use getch in Linux??
0
 
basantCommented:
I am also interested in Answer.
0
 
imladrisCommented:
Yes, getch is an ANSII standard library function.
0
 
ozoCommented:
getch is not ANSI C standard.
But it is available in <curses.h> on most Unix
0
 
basantCommented:
Yes getch is defined in <Dos.h>
when using MSDOS.
It is not an ANSI standard.

I think one can't write code for password reading, in standard C which is portable to different platforms. One has to use the specific terminal/ Screen calls.
0
 
imladrisCommented:
Sorry, I will defer to ozo. I didn't actually have my manual handy here to check with....
0
 
zultiCommented:
if you want to get the password from the user ( this is not only the example )
You can use the function:
    char* getpass (const char*)

here a small program using getpass on sun:

#include <stdio.h>
#include <string.h>

main()
{
        char pw[32+1];
        char *pass;

        pass=getpass("Enter your password: ");
        strcpy(pw, pass);
        printf(">> %s\n",pw);
        return 0;
}              
0
 
Lab_RatCommented:
N.B. not compiled or tested!

enter password:*****<?

With a flashing '?'!!!

void GetPassword(char *password,int maxlen)
{
int p=0;
char key='0';
for(;;)
  {
  do{int delay=0;if(delay==500){printf"<?";} if(delay==1000) {delay=0;printf("\08 \08\08";)}while(!kbhit())
  key=getch();
  if(key==13) {password[p]=0; return;}
  if((key==8)&&(p>0)) password[--p]=32;
  if((key>=32)&&(key<=95)&&(p<maxlen)) {password[p++]=key; printf("*");} else printf("\a");
  }
}
0
 
tmfrdCommented:
Might be stupid but, how about printing a backspace character followed by "*" to hide it? ^_^

scanf(" %c",&in);
printf("\b*");
0
All Courses

From novice to tech pro — start learning today.