getch?

Discussion in 'C Programming' started by Profetas, Nov 2, 2004.

  1. Profetas

    Profetas Guest

    Hi,

    How do I read a char from stdin before the '\n' is pressed?

    for example I know that I can use getch() to read a single char from the
    user input, but suppose I wanted to read before the carriage return is
    pressed.

    for example I type a char and the following actions is done before the
    '\n' is pressed.

    Thanks
    Profetas, Nov 2, 2004
    #1
    1. Advertising

  2. Profetas

    Chris Torek Guest

    In article
    <news:>
    Profetas <> wrote:
    >How do I read a char from stdin before the '\n' is pressed?


    See the comp.lang.c FAQ, question 19.1.
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
    Chris Torek, Nov 2, 2004
    #2
    1. Advertising

  3. Profetas

    Michael Mair Guest

    Profetas wrote:
    > Hi,
    >
    > How do I read a char from stdin before the '\n' is pressed?
    >
    > for example I know that I can use getch() to read a single char from the
    > user input, but suppose I wanted to read before the carriage return is
    > pressed.
    >
    > for example I type a char and the following actions is done before the
    > '\n' is pressed.


    FAQ 19.4

    Your questions *always* are FAQ slightly rewritten -- either you
    are doing this to keep us busy or you are too lazy to read the thing.
    Not fair. As promissed:

    *PLONK*
    --
    E-Mail: Mine is a gmx dot de address.
    Michael Mair, Nov 2, 2004
    #3
  4. Profetas

    Eric Sosman Guest

    Profetas wrote:
    > Hi,
    >
    > How do I read a char from stdin before the '\n' is pressed?


    This is Question 19.1 in the comp.lang.c Frequently
    Asked Questions (FAQ) list

    http://www.eskimo.com/~scs/C-faq/top.html

    > for example I know that I can use getch() to read a single char from the
    > user input, but suppose I wanted to read before the carriage return is
    > pressed.


    <OT>Has anyone else noticed that on this newsgroup
    the phrase "I know" almost always introduces a false
    statement, or at the very least a gross misunderstanding?
    I know I can't be the only one who-- oh, drat!</OT>

    There is no getch() function in Standard C. If your
    C implementation happens to provide one, it's some kind
    of extension to C, tacked on rather than inherent.

    > for example I type a char and the following actions is done before the
    > '\n' is pressed.


    FAQ.

    --
    Eric Sosman, Nov 2, 2004
    #4
  5. Profetas

    Michael Mair Guest

    > FAQ 19.4
    Corr: FAQ 19.1
    Sorry
    --
    E-Mail: Mine is a gmx dot de address.
    Michael Mair, Nov 2, 2004
    #5
  6. Profetas wrote:

    > Hi,
    >
    > How do I read a char from stdin before the '\n' is pressed?


    ITYM: "How do I read the FAQ?"

    > for example I know that I can use getch()


    There is no 'getch' in C. You know something that isn't true.

    > to read a single char from the
    > user input, but suppose I wanted to read before the carriage return is
    > pressed.
    >
    > for example I type a char and the following actions is done before the
    > '\n' is pressed.
    Martin Ambuhl, Nov 2, 2004
    #6
  7. Eric Sosman <> spoke thus:

    > <OT>Has anyone else noticed that on this newsgroup
    > the phrase "I know" almost always introduces a false
    > statement, or at the very least a gross misunderstanding?


    Which is why I typically forego using it; it lessens the sting when I
    turn out to be wrong ;)

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Nov 2, 2004
    #7
  8. Profetas

    Profetas Guest

    NAME
    getch, wgetch, mvgetch, mvwgetch, ungetch, has_key - get (or
    push back)
    characters from curses terminal keyboard

    SYNOPSIS
    #include <curses.h>

    int getch(void);
    int wgetch(WINDOW *win);
    int mvgetch(int y, int x);
    int mvwgetch(WINDOW *win, int y, int x);
    int ungetch(int ch);
    int has_key(int ch);

    DESCRIPTION
    The getch, wgetch, mvgetch and mvwgetch, routines read a
    character from
    the window. In no-delay mode, if no input is waiting, the value
    ERR is
    returned. In delay mode, the program waits until the system
    passes text
    through to the program. Depending on the setting of cbreak, this
    is after
    one character (cbreak mode), or after the first newline (nocbreak
    mode).
    In half-delay mode, the program waits until a character is typed
    or the
    specified timeout has been reached.
    Profetas, Nov 3, 2004
    #8
  9. Profetas <> scribbled the following:
    > NAME
    > getch, wgetch, mvgetch, mvwgetch, ungetch, has_key - get (or
    > push back)
    > characters from curses terminal keyboard


    > SYNOPSIS
    > #include <curses.h>


    Curses is a platform-specific extension to C and not a part of ISO
    standard C.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "How can we possibly use sex to get what we want? Sex IS what we want."
    - Dr. Frasier Crane
    Joona I Palaste, Nov 3, 2004
    #9
  10. Profetas

    Dan Pop Guest

    In <cma6ek$4sl$> Joona I Palaste <> writes:

    >Curses is a platform-specific extension to C and not a part of ISO
    >standard C.


    It is not part of ISO standard C, but it is the best you can get in terms
    of platform *independent* API for full screen programming. You're
    likely to find a curses implementation for every hosted implementation
    on which full screen programming makes sense.

    OTOH, it is ludicrous to use curses if all you want is the ability to
    get direct access to the user input.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Currently looking for a job in the European Union
    Dan Pop, Nov 3, 2004
    #10
  11. Profetas

    John Bode Guest

    "Profetas" <> wrote in message news:<>...
    > NAME
    > getch, wgetch, mvgetch, mvwgetch, ungetch, has_key - get (or
    > push back)
    > characters from curses terminal keyboard
    >
    > SYNOPSIS
    > #include <curses.h>
    >
    > int getch(void);
    > int wgetch(WINDOW *win);
    > int mvgetch(int y, int x);
    > int mvwgetch(WINDOW *win, int y, int x);
    > int ungetch(int ch);
    > int has_key(int ch);
    >
    > DESCRIPTION
    > The getch, wgetch, mvgetch and mvwgetch, routines read a
    > character from
    > the window. In no-delay mode, if no input is waiting, the value
    > ERR is
    > returned. In delay mode, the program waits until the system
    > passes text
    > through to the program. Depending on the setting of cbreak, this
    > is after
    > one character (cbreak mode), or after the first newline (nocbreak
    > mode).
    > In half-delay mode, the program waits until a character is typed
    > or the
    > specified timeout has been reached.



    Lovely. However, curses is not a part of the standard C library, and
    is not a part of every C implementation out there (I don't think
    you're going to find curses in MSVC). curses is an extension,
    provided by a specific implementation.

    C does not provide high-level (built-in) support for reading
    individual keystrokes; this is something that's specific to your
    particular platform, and this question would be more topical in a
    group devoted to your platform. The answer for *nix is different from
    the answer for Windows, which is different from the answer for VMS,
    etc.
    John Bode, Nov 3, 2004
    #11
  12. "John Bode" <> wrote in message
    news:...
    > "Profetas" <> wrote in message

    news:<>...
    > > NAME
    > > getch, wgetch, mvgetch, mvwgetch, ungetch, has_key - get (or
    > > push back)
    > > characters from curses terminal keyboard
    > >
    > > SYNOPSIS
    > > #include <curses.h>
    > >
    > > int getch(void);
    > > int wgetch(WINDOW *win);
    > > int mvgetch(int y, int x);
    > > int mvwgetch(WINDOW *win, int y, int x);
    > > int ungetch(int ch);
    > > int has_key(int ch);
    > >
    > > DESCRIPTION
    > > The getch, wgetch, mvgetch and mvwgetch, routines read a
    > > character from
    > > the window. In no-delay mode, if no input is waiting, the

    value
    > > ERR is
    > > returned. In delay mode, the program waits until the system
    > > passes text
    > > through to the program. Depending on the setting of cbreak, this
    > > is after
    > > one character (cbreak mode), or after the first newline

    (nocbreak
    > > mode).
    > > In half-delay mode, the program waits until a character is

    typed
    > > or the
    > > specified timeout has been reached.

    >
    >
    > Lovely. However, curses is not a part of the standard C library, and
    > is not a part of every C implementation out there (I don't think
    > you're going to find curses in MSVC). curses is an extension,
    > provided by a specific implementation.
    >
    > C does not provide high-level (built-in) support for reading
    > individual keystrokes; this is something that's specific to your
    > particular platform, and this question would be more topical in a
    > group devoted to your platform. The answer for *nix is different from
    > the answer for Windows, which is different from the answer for VMS,
    > etc.


    Mr. OP--

    Might you be so kind as to provide the text to the non-Standard header file
    in question? Around here, it seems to be the punchline to some joke I don't
    know. MPJ

    P.S. Honestly it sounds like Harry Potter. Isn't Migwetch one of the
    fraternities in which W was smart?
    Merrill & Michele, Nov 5, 2004
    #12
  13. Merrill & Michele <> scribbled the following:
    > "John Bode" <> wrote in message
    > news:...
    >> "Profetas" <> wrote in message

    > news:<>...
    >> > NAME
    >> > getch, wgetch, mvgetch, mvwgetch, ungetch, has_key - get (or
    >> > push back)
    >> > characters from curses terminal keyboard
    >> >
    >> > SYNOPSIS
    >> > #include <curses.h>


    (snip)

    >> Lovely. However, curses is not a part of the standard C library, and
    >> is not a part of every C implementation out there (I don't think
    >> you're going to find curses in MSVC). curses is an extension,
    >> provided by a specific implementation.
    >>
    >> C does not provide high-level (built-in) support for reading
    >> individual keystrokes; this is something that's specific to your
    >> particular platform, and this question would be more topical in a
    >> group devoted to your platform. The answer for *nix is different from
    >> the answer for Windows, which is different from the answer for VMS,
    >> etc.


    > Mr. OP--


    > Might you be so kind as to provide the text to the non-Standard header file
    > in question? Around here, it seems to be the punchline to some joke I don't
    > know. MPJ


    Merely having the header file won't help you. Header files don't contain
    code for functions - merely definitions. The actual code is stored in
    libraries, which aren't necessarily portable across implementations.
    This is all explained in the comp.lang.c FAQ.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "It sure is cool having money and chicks."
    - Beavis and Butt-head
    Joona I Palaste, Nov 5, 2004
    #13
  14. Profetas

    Mike Wahler Guest

    "Joona I Palaste" <> wrote in message
    news:cmfa4q$rdo$...
    > Merrill & Michele <> scribbled the following:
    > > Might you be so kind as to provide the text to the non-Standard header

    file
    > > in question? Around here, it seems to be the punchline to some joke I

    don't
    > > know. MPJ

    >
    > Merely having the header file won't help you. Header files don't contain
    > code for functions - merely definitions.


    Declarations. But you knew that. :)

    -Mike
    Mike Wahler, Nov 5, 2004
    #14
  15. Mike Wahler <> scribbled the following:
    > "Joona I Palaste" <> wrote in message
    > news:cmfa4q$rdo$...
    >> Merrill & Michele <> scribbled the following:
    >> > Might you be so kind as to provide the text to the non-Standard header

    > file
    >> > in question? Around here, it seems to be the punchline to some joke I

    > don't
    >> > know. MPJ

    >>
    >> Merely having the header file won't help you. Header files don't contain
    >> code for functions - merely definitions.


    > Declarations. But you knew that. :)


    Yes. Dammit Jim, I mean Mike, those words are too similar. In Finnish
    "definition" is "määritelmä" and "declaration" is "julistus". It would
    be so much better if we all spoke Finnish here. =)

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "No, Maggie, not Aztec, Olmec! Ol-mec!"
    - Lisa Simpson
    Joona I Palaste, Nov 5, 2004
    #15

  16. >>>> MPJ: Might you be so kind as to provide the text to the non-Standard

    header
    > > file
    > >> > in question? Around here, it seems to be the punchline to some joke

    I
    > > don't know.


    > >> "Joona I Palaste: Merely having the header file won't help you. Header

    files don't contain
    > >> code for functions - merely definitions.


    > > Mike Wahler : Declarations. But you knew that. :)


    > Joona: Yes. Dammit Jim, I mean Mike, those words are too similar. In

    Finnish
    > "definition" is "määritelmä" and "declaration" is "julistus". It would
    > be so much better if we all spoke Finnish here. =)


    Your use of dammit jim does not follow the standard prototype. It always
    goes, "Dammit Jim, I'm a doctor not a ...." And if we switch languages in
    clc, it's not going to be one where Pop has the drop on us. MPJ
    Merrill & Michele, Nov 5, 2004
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. azsx

    getch in Java

    azsx, May 3, 2006, in forum: Java
    Replies:
    5
    Views:
    9,438
  2. Matthew
    Replies:
    0
    Views:
    2,896
    Matthew
    Jun 28, 2003
  3. voidstar
    Replies:
    3
    Views:
    10,061
    Mark R Rivet
    Jul 27, 2004
  4. Shugong Wang
    Replies:
    2
    Views:
    9,593
    Raymond Martineau
    Nov 28, 2004
  5. aurgathor
    Replies:
    4
    Views:
    7,889
    Karl Heinz Buchegger
    Mar 16, 2005
Loading...

Share This Page