morrin
asked on
How do get my program to read in input from the terminal which I do not want to appear , i.e as in a password prompt
Hi
What I want is for my program to do something like , prompt for a password such as
%/
Pleas enter passwd ...
But while I want to read this in to check it , I don't want it to appear on the screen . i.e. I don't want anything to print to the screen until the user presses enter.
If anybody could tell me how to do this in C or using a script I would be grateful
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi
First up thanks for replying so quickly. However when I try to compile the code I get
/tmp/ccTsP5ye.o: In function `main':
/tmp/ccTsP5ye.o(.text+0x16
/tmp/ccTsP5ye.o(.text+0x21
/tmp/ccTsP5ye.o(.text+0x45
/tmp/ccTsP5ye.o(.text+0x73
/tmp/ccTsP5ye.o(.text+0x82
/tmp/ccTsP5ye.o(.text+0x92
Obviously I'm making some silly little error in compiling it . Any ideas on where I'm going wrong.
Thanks again
Try removing the underscores from in front of the function names.
Although, if you are using a *nix system, getch won't be available anyway.
ASKER
Yeah , Am using FreeBsd and Red H.Does getch do the same as getc?
LOL @ my MS world.
For C-compliant C code, remove the "_" before the functions' names.
_cputs(...) => cputs(...)
_getch(...) => getch(...)
_putch(...) => putch(...)
For C-compliant C code, remove the "_" before the functions' names.
_cputs(...) => cputs(...)
_getch(...) => getch(...)
_putch(...) => putch(...)
ASKER
No actually theres a man page for getch. Just in another header file
ASKER
Here , Thanks a million anyway. I can work on it from here. Here's the points for ya.
No... getch() doesn't echo. getc(stdin) / getchar() echos.
hi,
Why all this headache. Why don't u use getpass function to do this.
Why all this headache. Why don't u use getpass function to do this.
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#define MAX_CHARS 80
int main (void)
{
char password [MAX_CHARS + 1];
int index = 0;
char inchar;
_cputs("Enter your password: ");
do
{
inchar = _getch();
if (isprint(inchar))
{
if (index < MAX_CHARS)
{
password[index] = inchar;
index++;
}
else
{
_putch('\a');
}
}
else
{
if (inchar == '\b')
{
if (index > 0)
{
index--;
}
else
{
_putch('\a');
}
}
else if (inchar == '\n')
{
_getch();
}
}
} while (inchar != '\r');
password[index] = '\0';
_cputs("\n\nYou typed: ");
_cputs(password);
_cputs("\n");
return 0;
}