免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 8018 | 回复: 3
打印 上一主题 下一主题

[C] c语言中如何编写把输入数字显示成星号的代码?求高手解答 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-02-13 10:13 |只看该作者 |倒序浏览
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
2 [报告]
发表于 2009-02-13 10:57 |只看该作者
你的思路是不对的,需要设置终端参数为不回显字符,摘录apue的一个函数给你





  1. #include        <signal.h>
  2. #include        <stdio.h>
  3. #include        <termios.h>

  4. #define        MAX_PASS_LEN        8                /* max #chars for user to enter */

  5. char *
  6. getpass(const char *prompt)
  7. {
  8.         static char                buf[MAX_PASS_LEN + 1];        /* null byte at end */
  9.         char                        *ptr;
  10.         sigset_t                sig, osig;
  11.         struct termios        ts, ots;
  12.         FILE                        *fp;
  13.         int                                c;

  14.         if ((fp = fopen(ctermid(NULL), "r+")) == NULL)
  15.                 return(NULL);
  16.         setbuf(fp, NULL);

  17.         sigemptyset(&sig);
  18.         sigaddset(&sig, SIGINT);                /* block SIGINT */
  19.         sigaddset(&sig, SIGTSTP);                /* block SIGTSTP */
  20.         sigprocmask(SIG_BLOCK, &sig, &osig);        /* and save mask */

  21.         tcgetattr(fileno(fp), &ts);                /* save tty state */
  22.         ots = ts;                                                /* structure copy */
  23.         ts.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
  24.         tcsetattr(fileno(fp), TCSAFLUSH, &ts);
  25.         fputs(prompt, fp);

  26.         ptr = buf;
  27.         while ((c = getc(fp)) != EOF && c != '\n')
  28.                 if (ptr < &buf[MAX_PASS_LEN])
  29.                         *ptr++ = c;
  30.         *ptr = 0;                        /* null terminate */
  31.         putc('\n', fp);                /* we echo a newline */

  32.         tcsetattr(fileno(fp), TCSAFLUSH, &ots); /* restore TTY state */
  33.         sigprocmask(SIG_SETMASK, &osig, NULL);  /* restore mask */
  34.         fclose(fp);                        /* done with /dev/tty */
  35.         return(buf);
  36. }
复制代码

[ 本帖最后由 wwdwwd 于 2009-2-13 10:59 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2009-02-13 11:00 |只看该作者
这个不能使字符变成*,但是能禁止回显。使用方法如下:

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include "getpass.c"

//#include<curses.h>

char *getpass(const char *prompt);

int main()

{

                int k;

                int c;

                char *test_passwd;

                char name[20]="";

                printf("please input user name:\n");

                scanf("%s",name);

                getchar();

                  test_passwd = getpass("please input user password\n");

            printf("%s\n",test_passwd);

            return 0;

                                                                                                           

}

论坛徽章:
0
4 [报告]
发表于 2009-02-13 14:17 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP