macos 相当于 Windows getch() for Mac/Linux 崩溃
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/267250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Equivalent to Windows getch() for Mac/Linux crashes
提问by Jonathan Leffler
I am using getch()
and my app crashes instantly. Including when doing:
我正在使用getch()
,我的应用程序立即崩溃。包括在做的时候:
int main()
{
getch();
}
I can't find the link but supposedly the problem is that it needs to turn off buffering or something strange along those lines, and I still want cout
to work along with cross platform code.
我找不到链接,但据推测,问题是它需要关闭缓冲或沿着这些线路关闭一些奇怪的东西,我仍然想cout
使用跨平台代码。
I was told to use std::cin.get()
, but I'd like the app to quit when a key is pressed, not when the user typed in a letter or number then press enter to quit.
有人告诉我使用std::cin.get()
,但我希望应用程序在按下某个键时退出,而不是在用户输入字母或数字然后按 Enter 退出时退出。
Is there any function for this? The code must work under Mac (my os) and Windows.
有什么功能吗?代码必须在 Mac(我的操作系统)和 Windows 下工作。
Linking/compiling is not an issue; I include <curses.h>
and link with -lcurses
in XCode, while Windows uses <conio.h>
.
链接/编译不是问题;我在 XCode 中包含<curses.h>
并链接-lcurses
,而 Windows 使用<conio.h>
.
回答by Jonathan Leffler
Have you looked in <curses.h>
to see what the getch()
function does?
您是否查看过<curses.h>
该getch()
功能的作用?
Hint: OSX and Linux are not the same as Windows.
提示:OSX 和 Linux 与 Windows 不同。
Specifically, as a macro in <curses.h>
, we find:
具体来说,作为 中的宏<curses.h>
,我们发现:
#define getch() wgetch(stdscr)
Now, there appears, on your system, to be an actual function getch()
in the curses library, but it expects stdscr
to be set up, and that is done by the curses initialization functions (initscr()
and relatives), and that is signally not done by your code. So, your code is invoking undefined behaviour by calling curses routines before the correct initialization is done, leading to the crash.
现在,在您的系统上,getch()
curses 库中似乎有一个实际函数,但它期望stdscr
被设置,这是由curses 初始化函数(initscr()
和相关函数)完成的,而这显然不是由您的代码完成的. 因此,您的代码通过在正确的初始化完成之前调用curses 例程来调用未定义的行为,从而导致崩溃。
(Good hint from dmckee - it helped get the link line out of acidzombie24, which was important.)
(来自 dmckee 的好提示 - 它帮助从 acidzombie24 中获取链接,这很重要。)
To get to a point where a single key-stroke can be read and the program terminated cleanly, you have to do a good deal of work on Unix (OSX, Linux). You would have to trap the initial state of the terminal, arrange for an atexit()
function - or some similar mechanism - to restore the state of the terminal, change the terminal from cooked mode into raw mode, then invoke a function to read a character (possibly just read(0, &c, 1)
), and do your exit. There might be other ways to do it - but it certainly will involve some setup and teardown operations.
为了达到可以读取单个键击并且程序干净地终止的程度,您必须在 Unix(OSX、Linux)上做大量工作。您必须捕获终端的初始状态,安排一个atexit()
函数 - 或一些类似的机制 - 来恢复终端的状态,将终端从熟模式更改为原始模式,然后调用一个函数来读取一个字符(可能是只是read(0, &c, 1)
),然后退出。可能还有其他方法可以做到这一点 - 但它肯定会涉及一些设置和拆卸操作。
One book that might help is Advanced Unix Programming, 2nd Ednby Mark Rochkind; it covers terminal handling at the level needed. Alternatively, you can use <curses.h>
properly - that will be simpler than a roll-your-own solution, and probably more reliable.
可能有帮助的一本书是Mark Rochkind 的Advanced Unix Programming, 2nd Edn;它涵盖了所需级别的终端处理。或者,您可以<curses.h>
正确使用- 这比自己动手的解决方案更简单,而且可能更可靠。
回答by dmckee --- ex-moderator kitten
You have not exhibited a
你没有展示过
#include <stdio.h>
or
或者
#include <curses.h>
or similar line. Are you sure that you are linking against a library that includesgetch()
?
或类似的线路。您确定要链接到包含的库getch()
吗?
回答by Benjamin
Use the cin.get() function for example:
例如使用 cin.get() 函数:
#include <iostream>
using namespace std;
int main()
{
char input = cin.get();
cout << "You Pressed: " << input;
}
The program would then wait for you to press a key.
然后程序会等待你按下一个键。
Once you have, the key you pressed would be printed to the screen.
一旦你有了,你按下的键就会被打印到屏幕上。
回答by Jose Munoz
The getch
function is not available on Unix-like systems, but you can replace it with console commands through your compiler with the system
function.
该getch
函数在类 Unix 系统上不可用,但您可以通过带有该system
函数的编译器将其替换为控制台命令。
Usage:
用法:
In Windows you can use system("pause");
在 Windows 中,您可以使用 system("pause");
In Unix-like systems (such as OSX) you can use system("read -n1 -p ' ' key");
在类 Unix 系统(例如 OSX)中,您可以使用 system("read -n1 -p ' ' key");
Note: system
is declared in <stdlib.h>
.
注意:system
在<stdlib.h>
.