以前在Windows下好像就有过这种经验。在Eclipse里的java程序运行结果与命令行窗口的结果不一致。而往往是命令行窗口的结果是自己期望的。最近在学习LINUX,使用的是Eclpise+CDT环境。今天学习stty命令时,又一次出现了这种怪事。把代码贴出来,大家帮我分析一下原因吧。
1
#include
<
stdio.h
>
2 #include < termios.h >
3
4 void showbaud( int thespeed);
5 void show_some_flages( struct termios * ttyp);
6
7
8 int main()
9 {
10 struct termios ttyinfo;
11 if (tcgetattr( 0 , & ttyinfo) ==- 1 ) {
12 perror( " cannot get params about stdin. " );
13 exit( 1 );
14 }
15
16 showbaud(cfgetospeed( & ttyinfo));
17
18 printf( " the erase character is ascii %d, Ctrl - %c\n " ,
19 ttyinfo.c_cc[VERASE],ttyinfo.c_cc[VERASE] - 1 + ' A ' );
20 printf( " the line kill character is ascii %d, Ctrl - %c\n " ,
21 ttyinfo.c_cc[VKILL],ttyinfo.c_cc[VKILL] - 1 + ' A ' );
22
23 show_some_flages( & ttyinfo);
24
25 return 1 ;
26
27 }
28
29 void showbaud( int thespeed)
30 {
31 printf( " the baud rate is " );
32 switch (thespeed) {
33 case B300:printf( " 300\n " ); break ;
34 case B600:printf( " 600\n " ); break ;
35 case B1200:printf( " 1200\n " ); break ;
36 case B1800:printf( " 1800\n " ); break ;
37 case B2400:printf( " 2400\n " ); break ;
38 case B4800:printf( " 4800\n " ); break ;
39 case B9600:printf( " 9600\n " ); break ;
40 default :printf( " FAST\n " );
41 break ;
42 }
43
44 }
45
46 struct flaginfo{ int f1_value; char * f1_name;};
47
48 struct flaginfo input_flags[] = {
49 IGNBRK, " Ignore break condition " ,
50 BRKINT, " Signal interrupt on break " ,
51 IGNPAR, " Ignore chars with parity errors " ,
52 PARMRK, " Mark parity errors " ,
53 INPCK, " Enable input parity check " ,
54 ISTRIP, " Strip character " ,
55 INLCR, " Map NL to CR on input " ,
56 IGNCR, " Ignore CR " ,
57 ICRNL, " Map CR to NL on input " ,
58 IXON, " Enable start/stop output control " ,
59 IXOFF, " Enable start/stop input control " ,
60 0 ,NULL
61
62 };
63
64 struct flaginfo local_flags[] = {
65 ISIG, " Enable signals " ,
66 ICANON, " Cannonical upper/lower appearance " ,
67 ECHO, " Enable echo " ,
68 ECHOE, " Echo ERASE as BS-SPACE-BS " ,
69 ECHOK, " Echo KILL by starting new line " ,
70 0 ,NULL
71
72 };
73
74
75 void show_some_flages( struct termios * ttyp)
76 {
77 show_flagset(ttyp -> c_iflag,input_flags);
78 show_flagset(ttyp -> c_lflag,local_flags);
79 }
80
81 void show_flagset( int thevalue, struct flaginfo thebitnames[])
82 {
83 int i;
84 for (i = 0 ; thebitnames[i].f1_value; i ++ ) {
85 printf( " %s is " ,thebitnames[i].f1_name);
86 if (thevalue & thebitnames[i].f1_value)
87 printf( " ON\n " );
88 else
89 printf( " OFF\n " );
90 }
91
92 }
93
2 #include < termios.h >
3
4 void showbaud( int thespeed);
5 void show_some_flages( struct termios * ttyp);
6
7
8 int main()
9 {
10 struct termios ttyinfo;
11 if (tcgetattr( 0 , & ttyinfo) ==- 1 ) {
12 perror( " cannot get params about stdin. " );
13 exit( 1 );
14 }
15
16 showbaud(cfgetospeed( & ttyinfo));
17
18 printf( " the erase character is ascii %d, Ctrl - %c\n " ,
19 ttyinfo.c_cc[VERASE],ttyinfo.c_cc[VERASE] - 1 + ' A ' );
20 printf( " the line kill character is ascii %d, Ctrl - %c\n " ,
21 ttyinfo.c_cc[VKILL],ttyinfo.c_cc[VKILL] - 1 + ' A ' );
22
23 show_some_flages( & ttyinfo);
24
25 return 1 ;
26
27 }
28
29 void showbaud( int thespeed)
30 {
31 printf( " the baud rate is " );
32 switch (thespeed) {
33 case B300:printf( " 300\n " ); break ;
34 case B600:printf( " 600\n " ); break ;
35 case B1200:printf( " 1200\n " ); break ;
36 case B1800:printf( " 1800\n " ); break ;
37 case B2400:printf( " 2400\n " ); break ;
38 case B4800:printf( " 4800\n " ); break ;
39 case B9600:printf( " 9600\n " ); break ;
40 default :printf( " FAST\n " );
41 break ;
42 }
43
44 }
45
46 struct flaginfo{ int f1_value; char * f1_name;};
47
48 struct flaginfo input_flags[] = {
49 IGNBRK, " Ignore break condition " ,
50 BRKINT, " Signal interrupt on break " ,
51 IGNPAR, " Ignore chars with parity errors " ,
52 PARMRK, " Mark parity errors " ,
53 INPCK, " Enable input parity check " ,
54 ISTRIP, " Strip character " ,
55 INLCR, " Map NL to CR on input " ,
56 IGNCR, " Ignore CR " ,
57 ICRNL, " Map CR to NL on input " ,
58 IXON, " Enable start/stop output control " ,
59 IXOFF, " Enable start/stop input control " ,
60 0 ,NULL
61
62 };
63
64 struct flaginfo local_flags[] = {
65 ISIG, " Enable signals " ,
66 ICANON, " Cannonical upper/lower appearance " ,
67 ECHO, " Enable echo " ,
68 ECHOE, " Echo ERASE as BS-SPACE-BS " ,
69 ECHOK, " Echo KILL by starting new line " ,
70 0 ,NULL
71
72 };
73
74
75 void show_some_flages( struct termios * ttyp)
76 {
77 show_flagset(ttyp -> c_iflag,input_flags);
78 show_flagset(ttyp -> c_lflag,local_flags);
79 }
80
81 void show_flagset( int thevalue, struct flaginfo thebitnames[])
82 {
83 int i;
84 for (i = 0 ; thebitnames[i].f1_value; i ++ ) {
85 printf( " %s is " ,thebitnames[i].f1_name);
86 if (thevalue & thebitnames[i].f1_value)
87 printf( " ON\n " );
88 else
89 printf( " OFF\n " );
90 }
91
92 }
93
在eclipse中,运行结果为:
而在终端里面运行结果为:
事实上,终端里的结果是正确的。而ECLIPSE中的结果不正确。
我又在终端和ECLIPSE中多次运行了一下。每次结果都如上所示。
而Eclipse里的C编译器和终端里的一样,都是调用的GCC.那为什么结果会不一样呢?