Equivalent to Windows getch() for Mac/Linux crashes

我是研究僧i 提交于 2019-11-27 14:46:45

Have you looked in <curses.h> to see what the getch() function does?

Hint: OSX and Linux are not the same as Windows.

Specifically, as a macro in <curses.h>, we find:

#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.

(Good hint from dmckee - it helped get the link line out of acidzombie24, which was important.)

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.

One book that might help is Advanced Unix Programming, 2nd Edn by 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.

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 includes getch()?

Benjamin

Use the cin.get() function for example:

#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.

David 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.

Usage:

In Windows you can use system("pause");

In Unix-like systems (such as OSX) you can use system("read -n1 -p ' ' key");

Note: system is declared in <stdlib.h>.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!