2

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?

Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
uilianries
  • 3,363
  • 15
  • 28
  • 3
    Is this what you're trying to do? http://stackoverflow.com/questions/421860/c-c-capture-characters-from-standard-input-without-waiting-for-enter-to-be-pr – Mike Mar 07 '13 at 17:47
  • refr to http://stackoverflow.com/questions/7469139/what-is-equivalent-to-getch-getche-in-linux – MOHAMED Mar 07 '13 at 18:02

1 Answers1

4

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).

teppic
  • 8,039
  • 2
  • 24
  • 37
  • 1
    example of using termios http://stackoverflow.com/questions/7469139/what-is-equivalent-to-getch-getche-in-linux – MOHAMED Mar 07 '13 at 17:59