• C

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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
MatthewLCommented:
Marcoo - What's your O/S?

Matt
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

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
tzxie2000Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.