Thread: Emulating getch()?

  1. #1
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355

    Emulating getch()?

    From the ncurses programmers guide:
    One-character-a-time. To disable the buffering of typed characters by the TTY driver and get a character-at-a-time input, you need to call

    cbreak();
    The question is: How do you achieve the same functionality without reverting to non portable code? I tried:
    Code:
    setbuf(stdin, NULL);
    But still i couldn't disable the character buffering imposed by the system on standard keyboard input.
    Basically ncurses is a fine library, and can be used easily to create and run text applications, but what about a purely ANSI C approach?
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  2. #2
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101

  3. #3
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Thanks that fully answered my question.
    I almost re-implemented it by digging in the ncurses sources, and the cbreak() call.
    And i am glad to know that such matters such as keyboards and screens are not standard
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by xuftugulus View Post
    And i am glad to know that such matters such as keyboards and screens are not standard
    They are not made standard because C was intendet to work on platforms that may not have any keyboard or screen
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >setbuf(stdin, NULL);
    For future reference, the reason this doesn't work is because the stdio buffering mechanism is strictly for performance. Changing the buffering doesn't change the behavior of I/O.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 05:15 AM
  3. Clearing input buffer after using getch()
    By milkydoo in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2003, 11:04 PM
  4. Problems with getch()
    By GrNxxDaY in forum C++ Programming
    Replies: 14
    Last Post: 08-12-2002, 02:11 AM
  5. Using getch()
    By pr0ficient in forum Linux Programming
    Replies: 2
    Last Post: 07-26-2002, 05:59 AM