| 
 | 
菜单-4(终端控制示例)
 /*程序功能:读取每一个字符*/ 
 #include <stdio.h> 
 #include <unistd.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 *input; 
         FILE *output; 
         struct termios initial_settings, new_settings; 
  
         if(!isatty(fileno(stdout))) { 
                 fprintf(stderr, "You are not a terminal, OK.\\n"); 
         } 
  
         input = fopen("/dev/tty", "r"); 
         output = fopen("/dev/tty", "w"); 
         if(!input || !output) { 
                 fprintf(stderr, "Unable to open /dev/tty\\n"); 
                 exit(1); 
         } 
  
         /*进行终端各个参数的设置*/ 
  
         /*获取与标准输入相关联的终端控制属性*/ 
         tcgetattr(fileno(input), &initial_settings); 
  
         new_settings = initial_settings; /*转存*/ 
         new_settings.c_lflag &= ~ICANON; /*非标准模式*/ 
         new_settings.c_lflag &= ~ECHO;  /*不回显*/ 
         new_settings.c_cc[VMIN] = 1;    /*读到1个字符时就返回*/ 
         new_settings.c_cc[VTIME] = 0;   /*不设置时间影响*/ 
         new_settings.c_lflag &= ~ISIG;  /*终端不响应信号*/ 
  
         if(tcsetattr(fileno(input), TCSANOW, &new_settings) != 0) { 
                 fprintf(stderr, "could not set attributes\\n"); 
         } 
  
         do { 
                 choice = getchoice("Please select an action", menu, input, output); 
                 printf("You have chosen: %c\\n", choice); 
         } while(choice != 'q'); 
  
         tcsetattr(fileno(input), TCSANOW, &initial_settings); 
         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) {                    /*打印menu菜单*/ 
                         fprintf(stdout, "%s\\n", *option); 
                         option++; 
                 } 
  
                 do { 
                         selected = fgetc(in); /*读取一个字符,遇到回车或换行则继续读取*/ 
                 } while(selected == '\\n' || selected == '\\r'); 
                 option = choices; 
                 while(*option) { /*查询输入的字符和menu中的首字符是否一样*/ 
                         if(selected == *option[0]) { 
                                 chosen = 1; 
                                 break; 
                         } 
                         option++; 
                 } 
                 if(!chosen) { /*错误选择*/ 
                         fprintf(out, "Incorrect choice, select again\\n"); 
                 } 
                 } while(!chosen); /*错误选择则需重新输入*/ 
  
                 return selected; /*输入正确返回选择结果*/ 
 }  运行及输出:[root@localhost C]# ./password-2.exe  
choice: Please select an action 
a - add new record 
d - delete record 
q - quit 
You have chosen: a  #每次输入都会理解响应 
choice: Please select an action 
a - add new record 
d - delete record 
q - quit 
You have chosen: d 
choice: Please select an action #这里按下 Ctrl+C 测试,结果程序并不相应信号,这是因为程序中的 ~ISIG 的缘故 
a - add new record 
d - delete record 
q - quit 
Incorrect choice, select again 
choice: Please select an action 
a - add new record 
d - delete record 
q - quit 
You have chosen: q  |   
 
 
 
 |