Hi,
can anybody Write a program for Login that is while enter the password it should be print as * to corresponding character?
Thanks in advance,
srinivas k
Printable View
Hi,
can anybody Write a program for Login that is while enter the password it should be print as * to corresponding character?
Thanks in advance,
srinivas k
Yes programmers can generally do that.Quote:
can anybody Write a program for Login
If you're writing for Windows....:
Code:char *getPass(char *szBuffer, size_t len)
{
size_t i;
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dwOld, dwNumRead, dwNumWritten;
if((hIn != INVALID_HANDLE_VALUE) && (hOut != INVALID_HANDLE_VALUE))
{
if(GetConsoleMode(hIn,&dwOld))
{
if(SetConsoleMode(hIn,ENABLE_PROCESSED_INPUT))
{
for(i=0;i<len-1;i++)
{
if(ReadConsole(hIn,&szBuffer[i],1,&dwNumRead,NULL))
{
if(szBuffer[i] == '\r')
{
break;
}
else WriteConsole(hOut,"*",1,&dwNumWritten,NULL);
}
else break;
}
WriteConsole(hOut,"\n",1,&dwNumWritten,NULL);
szBuffer[i] = '\0';
SetConsoleMode(hIn,dwOld);
return szBuffer;
}
}
}
return NULL;
}
I've worked on this for days. The only way to get it to work was use the function above
Some said that if you're using visual studio you can just use getch() but I've never tried that.
I have tried using conio.h and getch() using GCC but they didn't work.
Do you really need to make it that portable? are you thinking of compiling it for anything other than windows?
No, this type of code will by it's nature use unportable functions. You may of course be able to use some sort of portable library to "hide" the unportable bits, e.g. ncurses or some such. But there are no standard C library functions to do this - there never has been, and there probably never will be, as there are certainly the possibility that some architectures don't even support this mode of operation.
--
Mats
Yes, that's not going to work - you WILL need some system specific code in there - there's no other way. This is because the C library doesn't have functions to turn off the echo (that is, there's no way to read characters from the keyboard without them also being printed to the screen), nor is there a way to "get one character at a time" in the standard library - it's all being processed by the OS into a line before it's being made available to the application. You need a way to bypass both the echo and the "hold it until a line is complee" to implement this, and there's just no way to do that without using system specific code. You can use third party libraries such as ncurses, but the standard C library will not do it. There's just no way.
--
Mats
Let's see if he gets it this time that it's not going to work.
To the OP: Your options are to either use a portable library that has separate implementations for most systems, to write your own, or to simply just use the one on the platform you need right now.
I've always wondered why there isn't a standard function to do this (like getch())? It certainly sounds like a fairly common thing that a lot of programs would need to do.
Probably because it's not guaranteed to be supported - and because once you get that far, you probably want some other control(s) over the input too, and this quickly gets system specific. There are libraries that support it this any much more, such as ncurses. It's just not part of the C standard.
--
Mats
No. Getchar reads a single character from the input stream - the printing is done by the underlaying OS's line input processing. Consider for example a teletype - it has no way to NOT print the character you entered [the way to "hide" passwords on a teletype is to read one char, then output "\10%\10*\10X\10\\\10$" or some such, so that the input gets thoroughly covered in ink]. Yes, I know, there aren't many teletypes around these days. There are, however, still some types of terminals [or emulators of such running on PC's] that do local line processing, and those would fall into the same criteria of not being able to do this.
--
Mats