免费注册 查看新帖 |

Chinaunix

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

一个土问题:unix下面如何无回显的读取用户输入 [复制链接]

iobuster 该用户已被删除
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-07-02 15:08 |只看该作者 |倒序浏览
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
2 [报告]
发表于 2004-07-02 16:26 |只看该作者

一个土问题:unix下面如何无回显的读取用户输入

供参考

  1. #include<stdlib.h>;
  2. #include<stdio.h>;
  3. #include<termios.h>;
  4. #include<string.h>;

  5. static struct termios stored_settings1;
  6. static struct termios stored_settings2;

  7. void echo_off(void)
  8. {
  9.    struct termios new_settings;
  10.    tcgetattr(0,&stored_settings1);
  11.    new_settings =stored_settings1;
  12.    new_settings.c_lflag &= (~ECHO);
  13.    tcsetattr(0,TCSANOW,&new_settings);
  14.    return;

  15. }

  16. void echo_on(void)
  17. {
  18.   tcsetattr(0,TCSANOW,&stored_settings1);
  19.   return;
  20. }


  21. void set_keypress(void)
  22. {
  23.   struct termios new_settings;
  24.   tcgetattr(0,&stored_settings2);
  25.   new_settings = stored_settings2;
  26.   
  27.   /*Disable canornical mode, and set buffer size to 1 byte */
  28.   new_settings.c_lflag&=(~ICANON);
  29.   new_settings.c_cc[VTIME] = 0;
  30.   new_settings.c_cc[VMIN] = 1;
  31.   
  32.   tcsetattr(0, TCSANOW, &new_settings);
  33.   return;

  34. }
  35. void reset_keypress(void)
  36. {
  37.   tcsetattr(0, TCSANOW, &stored_settings2);
  38.   return;
  39. }
  40. int main()
  41. {

  42.   printf("========================\n");
  43.   printf("0. Main menu\n");
  44.   printf("1. Chapter one\n");
  45.   printf("2. Chapter two\n");
  46.   printf("3. Chapter three\n");
  47.   printf("========================\n");
  48.   echo_off();
  49.   set_keypress();
  50.   char ch;
  51.   while((ch = getchar() ) != 'q')
  52.   {
  53.     switch(ch)
  54.     {
  55.       case '0':
  56.         printf("In main menu.\n");break;
  57.       case '1':
  58.         printf("Entering chapter one\n");break;
  59.       case '2':
  60.         printf("Entering chapter two\n");break;
  61.       case '3':
  62.         printf("Entering chapter three\n");break;
  63.       default:
  64.         printf("Entering other chapter.\n");
  65.      }
  66.    }  
  67.   reset_keypress();
  68.   echo_on();
  69.   return 0;

  70.   
  71. }

复制代码

论坛徽章:
0
3 [报告]
发表于 2004-07-02 16:31 |只看该作者

一个土问题:unix下面如何无回显的读取用户输入

最简单的方法是用getpass

论坛徽章:
0
4 [报告]
发表于 2004-07-02 18:14 |只看该作者

一个土问题:unix下面如何无回显的读取用户输入

用_getch,不要用getchar,估计就可以了。getch是没有echo的。

论坛徽章:
0
5 [报告]
发表于 2004-07-02 22:50 |只看该作者

一个土问题:unix下面如何无回显的读取用户输入

在UNIX的CRUSES编程中有提到,你去看看,只需关闭一个选项就可以了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP