• 对终端进行读写和与终端对话例程


      1 /*
      2 对终端进行读写的程序
      3 */
      4 
      5 #include <stdio.h>
      6 #include <unistd.h>
      7 
      8 char *menu[] = 
      9 {
     10     "a - add new record",
     11     "d - delete record",
     12     "q - quit",
     13     NULL,
     14 };//定义了字符串数组
     15 
     16 int getchoice(char *greet, char *choices[]);//定义了函数原型
     17 
     18 int main()//主函数
     19 {
     20     int choice = 0;
     21     
     22     if(!isatty(fileno(stdout)))//是终端的话返回1,不指执行循环体
     23     {
     24         fprintf(stderr,"You are not a terminal!
    ");
     25         exit(1);
     26     }
     27 
     28     do
     29     {
     30         choice = getchoice("Please select an action", menu);//调用函数
     31         printf("You have chosen: %c
    ", choice);//打印选择的字符串
     32     } while (choice != 'q');
     33     exit(0);
     34 }
     35 
     36 int getchoice(char *greet, char *choices[])//核心函数
     37 {
     38     int chosen = 0;
     39     int selected;
     40     char **option;
     41 
     42     do 
     43     {//先无条件循环一次
     44         printf("Choice: %s
    ",greet);//打印欢迎语
     45         
     46         option = choices;//数组赋值给option
     47         while(*option) 
     48         {//option不为空
     49             printf("%s
    ",*option);//循环打印option
     50             option++;
     51         }
     52         
     53         //selected = getchar();//获取用户输入单独这句在ubuntu上好像不能截取处合适的字符
     54         do{selected=getchar();}
     55         while (selected == '
    ');
     56         
     57         option = choices;//再次给option赋值为数组
     58         while(*option) 
     59         {//不为空的时候
     60             if(selected == *option[0]) //对比数组第一个字母和选择的输入
     61             {
     62                 chosen = 1;//
     63                 break;
     64             }
     65             option++;
     66         }
     67         if(!chosen) {//有匹配的话不执行
     68             printf("Incorrect choice, select again
    ");
     69         }
     70     } while(!chosen);//有匹配的话不再循环
     71     return selected;
     72 }
     73 //执行效果:
     74 /*
     75 jason@t61:~/c_program/544977-blp3e/chapter05$ ./a.out 
     76 Choice: Please select an action
     77 a - add new record
     78 d - delete record
     79 q - quit
     80 
     81 
     82 
     83 a
     84 You have chosen: a
     85 Choice: Please select an action
     86 a - add new record
     87 d - delete record
     88 q - quit
     89 d
     90 You have chosen: d
     91 Choice: Please select an action
     92 a - add new record
     93 d - delete record
     94 q - quit
     95 q
     96 You have chosen: q
     97 jason@t61:~/c_program/544977-blp3e/chapter05$ 
     98 
     99 jason@t61:~/c_program/544977-blp3e/chapter05$ ./a.out >file
    100 You are not a terminal!
    101 
    102 */
     1 #include <stdio.h>
     2 #include <unistd.h>
     3 
     4 char *menu[] = {
     5     "a - add new record",
     6     "d - delete record",
     7     "q - quit",
     8     NULL,
     9 };//定义了字符数组
    10 
    11 int getchoice(char *greet, char *choices[], FILE *in, FILE *out);//定义了函数原形
    12 
    13 int main()//主函数
    14 {
    15     int choice = 0;
    16     FILE * input;
    17     FILE * output;
    18 
    19     if (!isatty(fileno(stdout))) {//是终端的话返回-1执行循环体
    20         fprintf(stderr,"You are not a terminal, OK.
    ");
    21     }
    22 
    23     input = fopen("/dev/tty", "r");//只读模式打开终端。返回文件流
    24     output = fopen("/dev/tty", "w");//指写模式打开终端,返回文件流
    25     if(!input || !output) { //出错处理
    26         fprintf(stderr,"Unable to open /dev/tty
    ");
    27         exit(1);
    28     }
    29 
    30     do {
    31         choice = getchoice("Please select an action", menu, input, output);
    32         printf("You have chosen: %c
    ", choice);//调用函数,打印这句话
    33     } while (choice != 'q');
    34     exit(0);
    35 }
    36 
    37 int getchoice(char *greet, char *choices[], FILE *in, FILE *out)//核心函数
    38 {
    39     int chosen = 0;
    40     int selected;
    41     char **option;
    42 
    43     do {
    44         fprintf(out,"Choice: %s
    ",greet);
    45         option = choices;
    46         while(*option) 
    47         {
    48             fprintf(out,"%s
    ",*option);
    49             option++;//打印数组内容
    50         }
    51         
    52         do 
    53         {
    54             selected = fgetc(in);//从输入流获取输入到selected中
    55         } while (selected == '
    ');
    56         
    57         option = choices;
    58         while(*option)
    59          {//当数组不为空的时候
    60             if(selected == *option[0]) //匹配第一个字符和输入
    61             {
    62                 chosen = 1;
    63                 break;
    64             }
    65             option++;
    66         }
    67         if(!chosen) 
    68         {
    69             fprintf(out,"Incorrect choice, select again
    ");
    70         }
    71     } while(!chosen);
    72     return selected;//返回匹配的输入
    73 }
    74 
    75 //执行效果
    76 /*
    77 jason@t61:~/c_program/544977-blp3e/chapter05$ ./a.out >file1
    78 You are not a terminal, OK.
    79 Choice: Please select an action
    80 a - add new record
    81 d - delete record
    82 q - quit
    83 a
    84 Choice: Please select an action
    85 a - add new record
    86 d - delete record
    87 q - quit
    88 q
    89 jason@t61:~/c_program/544977-blp3e/chapter05$ cat file1
    90 You have chosen: a
    91 You have chosen: q
    92 
    93 */

    关于怎么重定向到文件中的还是没看懂。应该是跟/dev/tty的性质有关系吧,以后学到再研究。

    参考文献:

    Linux程序设计 Neil Matthew


    万事走心 精益求美


  • 相关阅读:
    第二阶段第二次spring会议
    第一次冲刺阶段的改进方案
    第二阶段第一次spring会议
    第七次spring会议
    第六次spring会议
    第五次spring会议
    第四次spring会议
    第三次spring会议
    第二次spring会议
    第一次spring会议
  • 原文地址:https://www.cnblogs.com/kongchung/p/4603706.html
Copyright © 2020-2023  润新知