01 #include <stdio.h>
02 #include <stdlib.h>
03
04 char *menu[] = {
05 "a - add new record",
06 "d - delete record",
07 "q - quit",
08 NULL,
09 };
10
11 int getchoice(char *greet, char *choices[]);
12
13 int main()
14 {
15 int choice = 0;
16 do
17 {
18 choice = getchoice("Please select an cation", menu);
19 printf("You have chosen: %c\n", choice);
20 }while(choice != 'q');
21
22 exit(0);
23 }
24
25 int getchoice(char *greet, char *choices[])
26 {
27 int chosen = 0;
28 int selected;
29 char **option;
30
31 do {
32 printf("Choice: %s\n", greet);
33 option = choices;
34 while(*option) { /*输出menu选项*/
35 printf("%s\n",*option);
36 option++;
37 }
38
39 selected = getchar(); /*获取用户输入*/
40 option = choices;
41
42 while(*option) { /*检查是否为a,d 或者 q*/
43 if(selected == *option[0]) {
44 chosen = 1;
45 break;
46 }
47 option++;
48 }
49
50 if(!chosen) {
51 printf("Incorrect choic, select again\n");
52 }
53
54 } while(!chosen);
55
56 return selected;
57 }
[root@localhost C]# ./menu-1.exe
Choice: Please select an cation
a - add new record
d - delete record
q - quit
a
You have chosen: a
Choice: Please select an cation
a - add new record
d - delete record
q - quit
Incorrect choic, select again
Choice: Please select an cation
a - add new record
d - delete record
q - quit
q
You have chosen: q
do {
selected = getchar(); /*获取用户输入*/
}while(selected == '\\n');
Choice: Please select an cation
a - add new record
d - delete record
q - quit
akq
You have chosen: a
Choice: Please select an cation
a - add new record
d - delete record
q - quit
Incorrect choic, select again
Choice: Please select an cation
a - add new record
d - delete record
q - quit
You have chosen: q
[root@localhost C]# ./menu-1.exe > menu-1-file
aq
[root@localhost C]# cat menu-1-file
Choice: Please select an cation
a - add new record
d - delete record
q - quit
You have chosen: a
Choice: Please select an cation
a - add new record
d - delete record
q - quit
You have chosen: q
#include <unistd.h>
int isatty(int fd);
01 #include <stdio.h>
02 #include <stdlib.h>
03 #include <unistd.h>
04
05 char *menu[] = {
06 "a - add new record",
07 "d - delete record",
08 "q - quit",
09 NULL,
10 };
11
12 int getchoice(char *greet, char *choices[]);
13
14 int main()
15 {
16 int choice = 0;
17
18 if(!isatty(fileno(stdout))) {
19 fprintf(stderr,"you are not a terminal!\\n");
20 exit(1);
21 }
22
23 do
24 {
25 choice = getchoice("Please select an cation", menu);
26 printf("You have chosen: %c\\n", choice);
27 }while(choice != 'q');
28
29 exit(0);
30 }
31
32 int getchoice(char *greet, char *choices[])
33 {
34 int chosen = 0;
35 int selected;
36 char **option;
37
38 do {
39 printf("Choice: %s\\n", greet);
40 option = choices;
41 while(*option) { /*输出menu选项*/
42 printf("%s\\n",*option);
43 option++;
44 }
45
46 do {
47 selected = getchar(); /*获取用户输入*/
48 }while(selected == '\\n');
49
50 option = choices;
51
52 while(*option) { /*检查是否为a,d 或者 q*/
53 if(selected == *option[0]) {
54 chosen = 1;
55 break;
56 }
57 option++;
58 }
59
60 if(!chosen) {
61 printf("Incorrect choic, select again\\n");
62 }
63
64 } while(!chosen);
65
66 return selected;
67 }
[root@localhost C]# ./menu-1.exe
Choice: Please select an cation
a - add new record
d - delete record
q - quit
q
You have chosen: q
[root@localhost C]# ./menu-1.exe > file
you are not a terminal!
[root@localhost C]# ./menu-1.exe >file 2>file.error
[root@localhost C]# cat file.error
you are not a terminal!
[root@localhost C]# ./menu-1.exe >file 2>&1
[root@localhost C]# cat file
you are not a terminal!
01 #include <stdio.h>
02 #include <unistd.h>
03 #include <stdlib.h>
04
05 char *menu[] = {
06 "a - add new record",
07 "d - delete record",
08 "q - quit",
09 NULL,
10 };
11
12 int getchoice(char *greet, char *choices[], FILE *in, FILE *out);
13
14 int main()
15 {
16 int choice = 0;
17 FILE *input;
18 FILE *output;
19
20 if(!isatty(fileno(stdout))) {
21 fprintf(stderr, "you are not a terminal, OK.\\n");
22 }
23
24 input = fopen("/dev/tty", "r");
25 output = fopen("/dev/tty", "w");
26 if(!input || !output) {
27 fprintf(stderr, "Unable to open /dev/tty\\n");
28 exit(1);
29 }
30
31 do {
32 choice = getchoice("Please select an action", menu, input, output);
33 printf("You have chosen: %c\\n", choice);
34 } while(choice != 'q');
35 exit(0);
36 }
37
38 int getchoice(char *greet, char *choices[], FILE *in, FILE *out)
39 {
40 int chosen = 0;
41 int selected;
42 char **option;
43
44 do {
45 fprintf(out, "Choice: %s\\n", greet);
46 option = choices;
47
48 while(*option) {
49 fprintf(out, "%s\\n", *option);
50 option++;
51 }
52
53 do {
54 selected = fgetc(in);
55 } while(selected == '\\n');
56
57 option = choices;
58
59 while(*option) {
60 if(selected == *option[0]) {
61 chosen = 1;
62 break;
63 }
64 option++;
65 }
66 if(!chosen) {
67 fprintf(out, "Incorrect choice, select again\\n");
68 }
69
70 }while(!chosen);
71
72 return selected;
73 }
[root@localhost C]# ./menu-2.exe > file
you are not a terminal, OK.
Choice: Please select an action
a - add new record
d - delete record
q - quit
almq #输入内容
Choice: Please select an action
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
Incorrect choice, select again
Choice: Please select an action
a - add new record
d - delete record
q - quit
[root@localhost C]# cat file
You have chosen: a
You have chosen: q
20 if(!isatty(fileno(stdout))) {
21 fprintf(stderr, "you are not a terminal, OK.\\n");
22 }
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |