linux上getch()出有关问题了
linux下getch()出问题了
我在linux下编写如下代码段:
#include <curses.h>
#include <stdio.h>
int
main(void)
{
char ch;
printf("Input a char:");
ch = getch();
printf("\nYou input a %c\n", ch);
return 0;
}
利用gcc -o testgetch testgetch.c -lcurses编译后无错,运行,并没有出现等待输入字符的情况,而是程序直接运行结束,网上也没找到出现这种情况的原因,望高手指教。
这就是运行状况:(运行后我没按任何键就变成这模样了)
[smellybb@localhost mycat]$ ./testgetch
Input a char:
You input a �
[smellybb@localhost mycat]$
gdb调试的结果是ch值为-1,感觉程序压根就没执行getch(),实在是理解不上去了……
------解决方案--------------------
都使用curse了
无法使用console的东西了
至少我没这么干过
------解决方案--------------------
你用:
#include<stdio.h>
#include<conio.h>
int
main(void)
{
char in;
printf("Input a char:");
fflush(stdin);
in = getchar();
printf("\nYou input a %c\n", in );
getch(); /*等待输入任一键*/
return 0;
}
------解决方案--------------------
我在linux下编写如下代码段:
#include <curses.h>
#include <stdio.h>
int
main(void)
{
char ch;
printf("Input a char:");
ch = getch();
printf("\nYou input a %c\n", ch);
return 0;
}
利用gcc -o testgetch testgetch.c -lcurses编译后无错,运行,并没有出现等待输入字符的情况,而是程序直接运行结束,网上也没找到出现这种情况的原因,望高手指教。
这就是运行状况:(运行后我没按任何键就变成这模样了)
[smellybb@localhost mycat]$ ./testgetch
Input a char:
You input a �
[smellybb@localhost mycat]$
gdb调试的结果是ch值为-1,感觉程序压根就没执行getch(),实在是理解不上去了……
------解决方案--------------------
都使用curse了
无法使用console的东西了
至少我没这么干过
------解决方案--------------------
你用:
#include<stdio.h>
#include<conio.h>
int
main(void)
{
char in;
printf("Input a char:");
fflush(stdin);
in = getchar();
printf("\nYou input a %c\n", in );
getch(); /*等待输入任一键*/
return 0;
}
------解决方案--------------------
- C/C++ code
#include <curses.h>
#include <stdio.h>
int
main(void)
{
char ch;
printf("Input a char:");
ch = getchar();
printf("\nYou input a %c\n", ch);
return 0;
}
------解决方案--------------------
<curses.h>
它的问题,我记得它有自已的输入输出函数,并且屏闭了<stdio.h>中的I/O函数
------解决方案--------------------
- C/C++ code
#include <stdio.h>
#include <curses.h>
int main(void)
{
char ch;
printf("Input a char: ");
initscr();
cbreak();
noecho();
ch = getch();
endwin();
printf("You input a %c\n", ch);
return 0;
}
建议man ncurses
------解决方案--------------------
- C/C++ code
getch()是个不可移植的函数,可以自己模拟出来一个。测试代码如下:
[root@cv0005759d1 stack]# cat testgetch.c
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
int getch(void);
int main(void)
{
char ch;
printf("Input a char:");
fflush(stdin);
ch = getch();
printf("\nYou input a %c\n", ch);
return 0;
}
int getch(void)
{
int c=0;
struct termios org_opts, new_opts;
int res=0;
//----- store old settings -----------
res=tcgetattr(STDIN_FILENO, &org_opts);
assert(res==0);
//---- set new terminal parms --------
memcpy(&new_opts, &org_opts, sizeof(new_opts));
new_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL);
tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);
c=getchar();
//------ restore old settings ---------
res=tcsetattr(STDIN_FILENO, TCSANOW, &org_opts);assert(res==0);
return c;
}
[root@cv0005759d1 stack]# gcc testgetch.c -o testgetch
[root@cv0005759d1 stack]# ./testgetch
Input a char:
You input a k
[root@cv0005759d1 stack]#