在linux下终端C编程做登陆时界面时,怎么在输入密码时让它不显示输入内容,或者输入一个字符是*

micaixiaoduanku 2011-09-04 01:10:45
如题,求教
...全文
605 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
惬意 2011-09-06
  • 打赏
  • 举报
回复
楼上的哥哥终端属性设置的这么牛X啊!
沭水河畔 2011-09-04
  • 打赏
  • 举报
回复
这个函数也可以自己实现:

#define ECHOFLAGS (ECHO|ECHOE|ECHOK|ECHONL)

int getpasswd(char passwd[])
{
int ret=0;
struct termios termios_buf;

if(tcgetattr(STDIN_FILENO,&termios_buf)!=0)
{
perror("tcgetattr failed");
return -1;
}

termios_buf.c_lflag &= ~ECHOFLAGS;

if(tcsetattr(STDIN_FILENO,TCSAFLUSH,&termios_buf)!=0)
{
perror("tcsetattr failed");
return -2;
}

if(passwd!=NULL){
ret=scanf("%s", passwd);
}

//printf("\nYour passwd is %s\n",passwd);

if(tcgetattr(STDIN_FILENO,&termios_buf)!=0)
{
perror("tcgetattr failed");
return -1;
}

termios_buf.c_lflag |= ECHOFLAGS;

if(tcsetattr(STDIN_FILENO,TCSAFLUSH,&termios_buf)!=0)
{
perror("tcsetattr failed");
return -2;
}

return ret>0?0:1;
}
delphiwcdj 2011-09-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 xharry 的回复:]

C/C++ code

#include <stdio.h>
#include <unistd.h>

int main()
{
char *passwd;

passwd = getpass("Enter your password:");

printf("\nThe password you entered:%s\n", passwd);

return 0……
[/Quote]
+1
念茜 2011-09-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 xharry 的回复:]

C/C++ code

#include <stdio.h>
#include <unistd.h>

int main()
{
char *passwd;

passwd = getpass("Enter your password:");

printf("\nThe password you entered:%s\n", passwd);

return 0……
[/Quote]
+1
xharry 2011-09-04
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <unistd.h>

int main()
{
char *passwd;

passwd = getpass("Enter your password:");

printf("\nThe password you entered:%s\n", passwd);

return 0;
}
justkk 2011-09-04
  • 打赏
  • 举报
回复
考虑调用getpass()
stardust20 2011-09-04
  • 打赏
  • 举报
回复
可以考虑用getch实现

23,215

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧