linux 终端 读取每个字符
来源:互联网 发布:矿油对皮肤有害吗 知乎 编辑:程序博客网 时间:2024/05/22 08:22
以下内容来自 beginning linux programming一书第5章 终端
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
char *menu[] = {
"a - add new record",
"d - delete record",
"q - quit",
NULL,
};
int getchoice(char *greet,char *choices[],FILE *in, FILE*out);
int main()
{
int choice = 0;
FILE * in;
FILE * out;
struct termios its,newits;
in = fopen("/dev/tty","r");
out = fopen("/dev/tty","w");
if(!in||!out)
{
fprintf(stderr,"fail to open /dev/tty\n");
exit(1);
}
tcgetattr(fileno(in),&its);
newits=its;
newits.c_lflag &= ~ICANON;
newits.c_lflag &= ~ECHO;
newits.c_cc[VMIN] = 1;
newits.c_cc[VTIME] = 0;
newits.c_lflag &= ~ISIG;
if(tcsetattr(fileno(in),TCSANOW,&newits))
{
fprintf(stderr,"could not set attr\n");
}
if(!isatty(fileno(stdout)))
{
fprintf(stderr,"You are not a terminal!\n");
// exit(1);
}
do
{
choice=getchoice("please select an action",menu,in,out);//,out);
printf("you have chosen: %c\n",choice);
}while(choice != 'q');
tcsetattr(fileno(in),TCSANOW,&its);
exit(0);
}
int getchoice(char *greet,char *choices[],FILE * in,FILE *out)
{
int chosen = 0;
int selected;
char **option;
do{
fprintf(out,"choice: %s\n",greet);
option=choices;
while(*option) //print menu
{
//printf("%s\n",*option);
fprintf(out,"%s\n",*option);
option++;
}
do{
selected = fgetc(in);
//selected = getchar();
}while(selected == '\n');//|| selected == '\r'); //encounter '\n' then loop on and on
option = choices;
while(*option)
{
if(selected == *option[0])
{
chosen = 1;
break;
}
option++;
}
if(!chosen)
{
fprintf(out,"incorrect choice,select again\n");
}
}while(!chosen);
return selected;
}
只要用户一键入字符就会立刻得到程序的响应,而且用户键入的字符不会回显。
- linux 终端 读取每个字符
- linux字符终端浏览器-----Lynx
- Linux字符终端彩色进度条
- Linux终端字符颜色设置
- linux字符终端浏览器--Lynx
- Linux终端字符颜色设置
- Linux 终端字符颜色设置
- 设置linux终端字符颜色
- LINUX终端字符颜色含义
- javascript读取出字符串中每个字符出现次数
- Linux终端彩色字符的输出方法
- [转]linux终端backspace不能删除字符
- Linux下字符终端分辨率设置
- linux 终端backspace不能删除字符
- linux终端 字符界面 显示乱码
- Linux直接启动到字符终端
- Linux终端彩色字符的输出方法
- Linux系统字符终端自动登录问题
- Android Matrix详解
- 走秀网陷入假货门
- Linux 中常用的文本(行)处理命令
- 三命通会 连载二
- linux 常见问题与命令记录(随时更新)
- linux 终端 读取每个字符
- (Android) Fragment
- ShareObject存储相关
- MySQL数据库 表结构的修改
- 一个c++的 http get 代码
- 闲来无事----中国的大国地位的讨论
- Python学习笔记-Python起步
- android iphone手机服务端接口(php实现,使用框架fat-free,解说例子blog)
- flash判断当前使用的浏览器类型