I have a getch()
function which my tutor gave me, which is getting input from the keyboard without clicking on 'ENTER'. But, when I run it in Ubuntu 12 in Eclipse, I get the following error:
tcsetattr(): Inappropriate ioctl for device
tcsetattr ICANON: Inappropriate ioctl for device
This is my code:
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
char getch();
int main(int argc, const char* argv[])
{
char c;
do
{
c=getch();
printf("%c",c);
} while(c!='q');
return 0;
}
char getch()
{
char buf = 0;
struct termios old = {0};
if (tcgetattr(0, &old) < 0)
perror("tcsetattr()");
old.c_lflag &= ~ICANON;
old.c_lflag &= ~ECHO;
old.c_cc[VMIN] = 1;
old.c_cc[VTIME] = 0;
if (tcsetattr(0, TCSANOW, &old) < 0)
perror("tcsetattr ICANON");
if (read(0, &buf, 1) < 0)
perror ("read()");
old.c_lflag |= ICANON;
old.c_lflag |= ECHO;
if (tcsetattr(0, TCSADRAIN, &old) < 0)
perror ("tcsetattr ~ICANON");
return (buf);
}
NOTE: The code DOES work in SSH Secure Shell. But I have to get this work in my Ubuntu, since I write my code there. Thanks