hi
how can i convert all the keys for the user how enter for example his passwort
to char ********
Printable View
hi
how can i convert all the keys for the user how enter for example his passwort
to char ********
It depends on your OS and compiler.
Do a board search, this is a common question.
Here's a Windows API version. Remember to include windows.h...
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) && (hOut))
{
if(GetConsoleMode(hIn,&dwOld))
{
if(SetConsoleMode(hIn,ENABLE_PROCESSED_INPUT))
{
for(i=0;i<len;i++)
{
if(ReadConsole(hIn,&szBuffer[i],1,&dwNumRead,NULL))
{
if(szBuffer[i] == '\r')
{
WriteConsole(hOut,"\n",1,&dwNumWritten,NULL);
szBuffer[i] = '\0';
break;
}
else WriteConsole(hOut,"*",1,&dwNumWritten,NULL);
}
else break;
}
SetConsoleMode(hIn,dwOld);
return szBuffer;
}
}
}
return NULL;
}
If using the WIN32 API, the edit control has a 'password' style ES_PASSWORD which will automatically replace text with asteriks.
but what is the size_t ......................
size_t is a data type in the standard C library, inside stdio.h. If size_t is not defined in windows.h, which I believe it is, you can include it in your C++ program by including cstdio.
GIYF.
I think size_t is associated with <cstddef>.
http://www.cplusplus.com/reference/c...ef/size_t.html
Edit: Actually the same site indicates that it's also in <cstdio>. I'm confused. Why is it guaranteed to be defined in more than one standard header file?
Edit: This is important to me since my code uses std::size_t a lot (and I always qualify it with std:: since I don't think it's guaranteed to be in the global namespace).
This site names 4 header files where size_t is define:
- stdio.h
- stddef.h
- stdlib.h
- string.h
The date type is important for strings, as well as malloc() and other functions, so I guess they chose the route of defining it quite a few times as opposed to just keeping it in one header file that all the others would be dependent upon.