C++ || Snippet – Custom Conio.h Sample Code For Linux

Print Friendly, PDF & Email

This page will consist of a brief implementation of “conio.h” for Linux.

Conio.h is a C header file used in old MS-DOS compilers to create text user interfaces. It is not part of the C standard library, ISO C, nor is it defined by POSIX. As a result, compilers that target Unix platforms do not contain this header file, and do not implement its library functions.

Included in the sample code are the following:

(1) getch() - Reads a character directly from the console without buffer, and without echo

(2) getche() - Reads a character directly from the console without buffer, but with echo.

(3) kbhit() - Determines if a keyboard key was pressed.

(4) clrscr() - Clears data from the console screen.


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

Note: Not all functions in conio.h are included in the above sample code.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

===== DEMONSTRATION HOW TO USE =====

Use of the above header file is the same as its DOS counterpart, and can be utilized by simply naming the above snippet as “conio.h.” Here is a sample program demonstrating its use.


Once compiled, you should get this as your output

Press a key!
Press a key!
Press a key!
Press a key!
Press a key!
Press a key!
Press a key!
Press a key!
Press a key!
Press a key!
Press a key!
Press a key!
p
You pressed 'p'!

The screen will now clear
Press any key to continue..

[ THE SCREEN CLEARS HERE ]

The screen successfully cleared
Press any key to continue..

Was this article helpful?
👍 YesNo

One Response to C++ || Snippet – Custom Conio.h Sample Code For Linux

  1. Avatar Rohit says:

    Hello,

    if you can write comment about each statement in the getch(), it would be really helpful. Actually I am new to termios and don’t know much about its functionality to set the terminal. If possible explain the snippet of getch() in much detailed manner so that it would be easy for me to read and understand how the process is going on.

    With regards,

    Rohit

Leave a Reply to RohitCancel reply