Most of the students are taught C/C++ with Turbo C/C++ IDE in schools. Turbo C has a header file(unspecified in ANSI standard) <conio.h> with which programmer can program for unbuffered input using functions like getch(). Also there are other functions like gotoxy() etc. which work perfect in DOS environment with Turbo C.
After they learn about linux, and then GCC they start practicing in linux. If you are such an student, read on. This will be handy for you.
At some point of time you will write a program in which you want interactive user input from the user. Interactive in the sense that you want an action to be performed as soon as a key is pressed. But since standard library function getchar() buffers the input until ENTER key is pressed, it will be quite irritating for you. So, you'll miss your <conio.h> header file that you used in Turbo. Don't worry. I faced the same problem and I ran around like everything to find a soluion. And finally, I found a solution!!!
Solution
Just download the libconioh from this link
Install it. Here are the steps(I'm considering Ubuntu as the example):
1. Copy the downloaded archive to your home folder
2. Open the terminal, by pressing Alt+F2 and typing xterm and pressing enter.
3. Become root by typing
sudo -sand supplying your password.
4. Type
tar -zxvf libconio-1.0.0.tar.gzand press enter to extract the archive. To make the typing easy you may press the <tab> key on your keyboard after typing l of libconio-1.0.0.tar.gz for autocompletion of filename.
5. Now enter into the extracted directory by typing
cd libconio-1.0.0and pressing enter.
6. Install the library by typing these commands one after another's execution.
./configure
make
make installNow the library has been installed. How to use it for programming?
Just #include <conio.h> in your program. And when compiling your program(eg. myprogram.c in current working directory) using gcc give the library's detail to the linker like this:
gcc myprogram.c -lconioYou will get the a.out file if your program doesn't have any errors.
Execute it and enjoy!
./a.outBut what did that -lconio did? See, in gcc the source files and object library files can be linked by specifying them back to back. Like:
gcc file1.c file2.c lib1.a lib2.aIn linux the library files are with .a extension and they are stored in /usr/lib/ directory with the name format lib<library_name>.a . So when you installed libconioh it copied a file, named libconio.la to the /usr/lib directory. Now you can compile and link your program that uses conio.h by this command
gcc myprogram.c /usr/lib/libconio.aThe -l switch has been provided as a shortcut for your convenience. This -l makes conio to be read as /usr/lib/libconio.a hence when yo typed gcc myprogram.c -lconio it was interpreted as gcc myprogram.c /usr/lib/libconio.a . Now thats sweet! Isn't it?
Here is a sample program that reads characters from the keyboard, displays the character just next to it(for example, if u enter b it will display c) and exits only when you press the small a key.
#include <conio.h>
int main(){
char c;
while((c=getch())!='a'){
putchar(c+1);
}
putchar('\n');
return 0;
}