Linux终端编程--判断是否为终端
2015-09-28 21:05
676 查看
干货,代码贴出来:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
char *menu[] =
{
"a - add new record",
"d - delete record",
"q -quit",
NULL,
};
int getchoice(char *greet, char *choicess[]);
int main()
{
int choice = 0;
if (!isatty(fileno(stdout)))
{
fprintf(stderr, "You are not a terminal!\n");
exit(1);
}
do
{
choice = getchoice("Please select an action",menu);
printf("You have chosen: %c\n", choice);
}while(choice != 'q');
exit(0);
}
int getchoice(char *greet, char *choices[])
{
int chosen = 0;
int selected;
char **option;
do
{
printf("Choice : %s\n", greet);
option = choices;
while (*option)
{
printf("%s\n",*option);
option++;
}
do{
selected = getchar();
option = choices;
}while(selected =='\n');
while(*option)
{
if (selected == *option[0])
{
chosen = 1;
break;
}
option++;
}
if(!chosen)
{
printf("Incorrect choice, select again \n");
}
}
while(!chosen);
return selected;
}执行结果如下:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
char *menu[] =
{
"a - add new record",
"d - delete record",
"q -quit",
NULL,
};
int getchoice(char *greet, char *choicess[]);
int main()
{
int choice = 0;
if (!isatty(fileno(stdout)))
{
fprintf(stderr, "You are not a terminal!\n");
exit(1);
}
do
{
choice = getchoice("Please select an action",menu);
printf("You have chosen: %c\n", choice);
}while(choice != 'q');
exit(0);
}
int getchoice(char *greet, char *choices[])
{
int chosen = 0;
int selected;
char **option;
do
{
printf("Choice : %s\n", greet);
option = choices;
while (*option)
{
printf("%s\n",*option);
option++;
}
do{
selected = getchar();
option = choices;
}while(selected =='\n');
while(*option)
{
if (selected == *option[0])
{
chosen = 1;
break;
}
option++;
}
if(!chosen)
{
printf("Incorrect choice, select again \n");
}
}
while(!chosen);
return selected;
}执行结果如下:
相关文章推荐
- Linux伙伴系统算法--防止内存碎片的产生
- Linux学习笔记——系统启动流程
- 利用linux系统命令分析PHP程序
- 5233杨光--Linux第二次实验
- 19、linux软件管理之简述
- linux定时任务的设置
- linux系统中设备文件命名的规则
- tty和pts解析
- 0-9 Linux远程登录
- Vmware安装CentOS
- linux配置samba服务器和windows进行数据交互
- 0-7 文本编辑器的使用
- linux 内核编译
- linux中特效地(显示颜色、粗体、下划线等效果)将文本输出到终端
- Linux教程之Linux用户和用户组管理
- Centos 搭建java运行环境
- Linux学习笔记——进程查看及管理
- SNMP在linux下的移植
- CentOS 6.6下安装OpenOffice4.0
- Linux_屏蔽360、scanv、QQ管家等IP扫描