Find the answer to your Linux question:
Results 1 to 6 of 6
Hi all, How do you get around buffered input in C? For example, if I were to write a shell with fgetc, I'd have no way to tell if the ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Engineer Javasnob's Avatar
    Join Date
    Jul 2005
    Location
    Wisconsin
    Posts
    942

    Getting around buffered input


    Hi all,

    How do you get around buffered input in C? For example, if I were to write a shell with fgetc, I'd have no way to tell if the user were trying to use tab-completion.

    Thanks in advance!
    Flies of a particular kind, i.e. time-flies, are fond of an arrow.

    Registered Linux User #408794

  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Gotcha covered, man. My first thought was simply to unbuffer stdin, but that didn't work. Nor did using low-level IO.

    However, I discovered that by changing the mode of the stdin file descriptor, you can actually get a raw stream, which returns each character to you as it is input. The easiest way to do it is by using the cfmakeraw() function in unistd.h. The process is described in the glibc documentation at:
    http://www.gnu.org/software/libc/man...anonical-Input

    Read some of the other information they have on changing modes: it's pretty interesting stuff.

    EDIT:

    When testing raw streams, make sure that you set it back to a non-raw stream afterwards!! I just F'd mine up by not doing that.

  3. #3
    Linux Engineer Javasnob's Avatar
    Join Date
    Jul 2005
    Location
    Wisconsin
    Posts
    942
    Sweet, thanks Cabhan! Is there a way to do it in Java as well, by chance?
    Flies of a particular kind, i.e. time-flies, are fond of an arrow.

    Registered Linux User #408794

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Hm. It's sad: I haven't done any Java programming in so long...C's been taking up all my time.

    I honestly don't know: I've never had to go so low-level in Java. The InputStream class definitely doesn't have anything for it, nor does java.nio.Buffer.

    There may be a way, but I don't know it.

  6. #5
    Linux Engineer Javasnob's Avatar
    Join Date
    Jul 2005
    Location
    Wisconsin
    Posts
    942
    Thanks again, Cabhan; I've done a simple implementation in Java using the JNI. Anyone can feel free to send me a private message for the code.
    Flies of a particular kind, i.e. time-flies, are fond of an arrow.

    Registered Linux User #408794

  7. #6
    Linux Engineer Javasnob's Avatar
    Join Date
    Jul 2005
    Location
    Wisconsin
    Posts
    942
    Sweet, that works perfectly. Now I've run into another issue: How would you undelete chars from a stream like bash does when it encounters a backspace?

    Thanks for all the help!
    Flies of a particular kind, i.e. time-flies, are fond of an arrow.

    Registered Linux User #408794

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •