• C

password entry in console application

Dear all,
I am developing a console based application and it requires a password entry field. I want that password should not be printed in screen (rather some character should be displayed instead)
eg.
Enter password : *****
Please comment!
replylalitAsked:
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.

umangjoshiCommented:
use setattrib() function

or use getch() to get one by one character in a password string
0
umangjoshiCommented:
may be setattr() function
0
akshayxxCommented:
this  works on windows... on linux u need slightly special stuff

#include<stdio.h>
#include<conio.h>

void main()
{
int a,i;
char c,buf[32];

printf(" input password.... ");
i=0;
while((c=getch())!='\r'){
     printf("%c",'*');
buf[i++]=c;
}
buf[i]='\0';
printf("\nu entered password %s\n",buf);
}
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ExceterCommented:
That setup works perfectly except for the fact that it does not support the backspace key. Try this,

#include <stdio.h>
#include <conio.h>
#define STRLEN 25

void input( char*, int );

int main()
{
    char s[STRLEN];
    printf("Enter password: ");
    input(s,STRLEN);
       printf("\n\n%s\n",s);
    return 0;
}

void input( char* buffer, int length )
{
  int pos = 0;
  char c;

  buffer[0] = '\0';

  while( ( c = getch() ) != 13 )
  {
      if( c != 8 && pos < ( length - 1 ) )
      {
          printf("*");
          buffer[pos] = c;
          pos++;
      }
      else if( pos != 0 && c == 8 )
      {
          printf("%c %c",8,8);
          pos--;
      }
  }
   buffer[pos] = '\0';
}

Exceter
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
Mayank SAssociate Director - Product EngineeringCommented:
Read the characters using getch () in a loop and keep storing them in an array, and keep displaying '*' for every character entered.

Mayank.
0
ExceterCommented:
>> Read the characters using getch () in a loop and keep storing them in an array, and keep displaying '*' for every character entered.

That is EXACTLY what the previously posted code does. :-)

Exceter
0
akshayxxCommented:
>>>That is EXACTLY what the previously posted code does. :-)

and  the prior to that one too :P

strange stats .. today most of the notifications that came to me..80 % were comment from Mayankeagle..
and many of them were similar like this one ... repeated ideas..
0
ExceterCommented:
>> today most of the notifications that came to me..80 % were comment from Mayankeagle..
>> and many of them were similar like this one ... repeated ideas..

I noticed that same phenomenon..

Exceter
0
gotenksCommented:
try this :

void input() {
   char c[8];
   int index = 0;

   while (1) {
      c[index] = getch();
      printf("*");
      index++;
      if (index == 8) {
         c[index] = '\0';
         break;
      }    
   }
   
/*   printf("%s\n", c); */
}

this should do the trick, i hope. anyway, it is just a sample that will accept input of 8 characters and exit the while loop.
0
gotenksCommented:
oopps... sorry, didnt notice that it is the same...
0
gotenksCommented:
oopps... sorry, didnt notice that it is the same...
0
Mayank SAssociate Director - Product EngineeringCommented:
How about rating it now?
0
jmcgOwnerCommented:
Nothing has happened on this question in more than 8 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by Exceter.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.