Reputation: 3889
I need read the stdin in Linux, although my program will receive only messages without new line.
I tried this code, but is not working:
int main ( void )
{
char p_char[48];
memset( p_char, 0, sizeof(p_char) );
fcntl( STDIN_FILENO, F_SETFL, FNDELAY );
read( STDIN_FILENO, p_char, sizeof(p_char) );
}
Someone have a suggestion?
Upvotes: 2
Views: 2017
Reputation: 8205
You'd need to change the terminal settings so that each character is sent immediately. You can do it by manipulating termios
(the man page has details).
Essentially it just involves creating two termios
structures, initialising one with the current settings with tcgetattr
, copying the struct to the other structure, modifying the buffer setting in it, and then setting the terminal with the new struct with tcsetattr
(and of course, setting it back when you're done).
Upvotes: 4