5

Previously I use c++/c compilers on windows which support the #include <conio.h> header file but on Linux where I have

gcc (Debian 4.9.2-10) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software...

I want a function which works exactly as getch(). I don't know why my compiler doesn't support the header file #include <conio.h>

After searching on net I got this which says that cin.get(); is probably the closest equivalent but these two are different in the way that if we write getch() it does not display the character entered on the console whereas if we enter a character using cin.get() it displays the character on the console. I don't want the character to be displayed on the console.

using getchar() also displays the character on the console.

jww
  • 97,681
  • 90
  • 411
  • 885
  • See this: http://stackoverflow.com/questions/3276546/how-to-implement-getch-function-of-c-in-linux – Danny_ds Dec 26 '15 at 19:53
  • 2
    This is what happens when you are taught ancient, platform-specific APIs instead of standard C++. – Lightness Races in Orbit Dec 26 '15 at 20:04
  • @LightnessRacesinOrbit does this curses.h comes into standard c++ and conio.h doesn't? –  Dec 26 '15 at 20:07
  • 1
    Neither `conio` nor `curses` are standard C libraries. The first is for windows, the other for linux (although there might be vice-versa implementations). – Weather Vane Dec 26 '15 at 20:08
  • 3
    Please choose one of C and C++. – fuz Dec 26 '15 at 21:08
  • 1
    @WeatherVane `curses` is an industrial standard library (part of SUS) and not “for Linux,” it's a highly portable software that runs on countless operating systems. – fuz Dec 26 '15 at 21:10
  • If you do not wish to use curses, have a look at http://stackoverflow.com/questions/7469139/what-is-equivalent-to-getch-getche-in-linux – cup Dec 26 '15 at 23:26
  • BTW, it's not a compiler issue. gcc would happily include `` if such a file existed. – Erich Kitzmueller Feb 14 '17 at 08:28
  • @WeatherVane Curious, I was using `curses.h` in the 1980s, long before Linux was even thought of. Unix, yes, Linux-specific, no. – user207421 Feb 14 '17 at 09:23
  • @EJP ok thank you. – Weather Vane Feb 14 '17 at 18:21
  • 1
    Possible duplicate of [What is Equivalent to getch() & getche() in Linux?](https://stackoverflow.com/questions/7469139/what-is-equivalent-to-getch-getche-in-linux) – Aconcagua Jul 10 '17 at 06:37

2 Answers2

5

There are a number of different ways of doing this more portably. The simplest is to use curses:

#include "curses.h"

int main() {
    initscr();
    addstr("hit a key:");
    getch();
    return endwin();
}
Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • 1
    can you explain `initscr();` ? –  Dec 26 '15 at 20:05
  • 1
    @Lovely: it initializes the curses library and is needed before calling any other function. `endwin()` then shuts things down and needs to be called before the program exits. – Chris Dodd Dec 26 '15 at 20:10
0

There is a thread answering this question with the following code:

#include <stdio.h>
#include <termios.h>

char getch() {
    char buf = 0;
    struct termios old = { 0 };
    fflush(stdout);
    if (tcgetattr(0, &old) < 0) perror("tcsetattr()");
    old.c_lflag    &= ~ICANON;   // local modes = Non Canonical mode
    old.c_lflag    &= ~ECHO;     // local modes = Disable echo. 
    old.c_cc[VMIN]  = 1;         // control chars (MIN value) = 1
    old.c_cc[VTIME] = 0;         // control chars (TIME value) = 0 (No time)
    if (tcsetattr(0, TCSANOW, &old) < 0) perror("tcsetattr ICANON");
    if (read(0, &buf, 1) < 0) perror("read()");
    old.c_lflag    |= ICANON;    // local modes = Canonical mode
    old.c_lflag    |= ECHO;      // local modes = Enable echo. 
    if (tcsetattr(0, TCSADRAIN, &old) < 0) perror ("tcsetattr ~ICANON");
    return buf;
 }

It uses termios driver interface library wich is part of POSIX (IEEE 1003.1)

For more references about this library read: The header contains the definitions used by the terminal I/O interfaces

I hope it helps you.

omotto
  • 1,721
  • 19
  • 20
  • I encountered this `warning: implicit declaration of function ‘read’; did you mean ‘fread’? [-Wimplicit-function-declaration] 15 | if (read(0, &buf, 1) < 0) perror("read()"); | ^~~~ | fread ` – rish_hyun Aug 25 '20 at 14:54
  • 1
    `read` is a syscall with prototype provided when you `#include `. Though I would recommend a method that doesn't need to change the keyboard mode for every character added. Better to put the keyboard into "raw" (non-canonical) mode once, take all input needed in raw mode and then return to "cooked" (canonical) mode. – David C. Rankin Jun 10 '21 at 21:14