about getch()

Discussion in 'C Programming' started by fuzhen, Jun 12, 2008.

  1. fuzhen

    fuzhen Guest

    If I want to "press any key to continue" in Windows, I can use getch()
    But the getch() isn't a standard C function
    So what should I to do in Linux?
     
    fuzhen, Jun 12, 2008
    #1
    1. Advertising

  2. fuzhen

    rahul Guest

    On Jun 12, 8:56 am, fuzhen <> wrote:
    > If I want to "press any key to continue" in Windows, I can use getch()
    > But the getch() isn't a standard C function
    > So what should I to do in Linux?


    getch() is a non-standard extension which does not echo the character
    entered. In standard C, you can use getchar(), getc(), fgetc() ....
    All these functions read a character. In Windows, the common practice
    is to use getch() for halting the program. Both conio.h and getch()
    are non-standard extensions. To be on the safer side, you can always
    use getchar() if you are trying to write portable code.
     
    rahul, Jun 12, 2008
    #2
    1. Advertising

  3. fuzhen wrote:
    > If I want to "press any key to continue" in Windows, I can use
    > getch()


    Why would you want to do that?

    Just run your program from a shell/dos window and avoid messy
    functions that just clutter your code and render it next to useless
    for batch scripts.

    --
    Peter
     
    Peter Nilsson, Jun 12, 2008
    #3
  4. Eric Sosman <> writes:
    > fuzhen wrote:
    >> If I want to "press any key to continue" in Windows, I can use getch()
    >> But the getch() isn't a standard C function
    >> So what should I to do in Linux?

    >
    > "Press any key other than the one you will press next."
    >
    > "Press ENTER" is also a candidate.


    "Press any key to continue. Press any other key to quit."

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jun 12, 2008
    #4
  5. fuzhen <> writes:
    > If I want to "press any key to continue" in Windows, I can use getch()
    > But the getch() isn't a standard C function
    > So what should I to do in Linux?


    As Peter Nilsson points out, it's often just not necessary,
    particularly if the "press any key to continue" occurs just before the
    program terminates.

    If you want the program to continue immediately after the user presses
    *any* key, there's no portable solution, though there are various
    non-portable solutions. See question 19.1 of the comp.lang.c FAQ,
    <http://c-faq.com/>.

    If you'll settle for requiring the user to press <return> (or <enter>,
    or whatever you want to call it), you can have a loop that reads and
    discards characters until it sees a '\n' (or EOF).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jun 12, 2008
    #5
  6. On 12 Jun 2008 at 3:56, fuzhen wrote:
    > If I want to "press any key to continue" in Windows, I can use getch()
    > But the getch() isn't a standard C function
    > So what should I to do in Linux?


    In most Linux shells, terminal input is usually line-buffered, so your
    program won't find out about character presses by default until the user
    presses return. Your main choices are:

    1) fiddle with functions in termios.h to switch the terminal to
    unbuffered mode, then call getchar(), then set the terminal back to
    buffered mode
    2) make a (n)curses application and use getch()
    3) settle for "press return to continue" and then do something to read a
    line, e.g. system("read");
     
    Antoninus Twink, Jun 12, 2008
    #6
  7. fuzhen

    Guest

    On Jun 12, 10:49 am, Mark McIntyre <>
    wrote:
    > CBFalconer wrote:
    > > fuzhen wrote:
    > >> If I want to "press any key to continue" in Windows, I can use
    > >> getch(). But the getch() isn't a standard C function. So what
    > >> should I to do in Linux?

    >
    > > Note the addition of periods to terminate sentences.

    >
    > carriage returns are also an acceptable way to indicate natural breaks
    > in writing.
    >
    > > Try "Press 'return' to continue" followed by "fflush(stdin);"

    >
    > Hmm. Why try undefined behaviour?

    I think he meant fflush(stdout), so "Press 'return' to continue" is
    actually written to the stdout stream.
    I also believe that it becomes more clear when you read the rest of
    his post (ie, the part you snipped)
     
    , Jun 12, 2008
    #7
  8. fuzhen

    Bill Reid Guest

    Keith Thompson <> wrote in message
    news:...

    > If you'll settle for requiring the user to press <return> (or <enter>,
    > or whatever you want to call it), you can have a loop that reads and
    > discards characters until it sees a '\n' (or EOF).


    Yes, and you could call that "loop" "gets()" or "fgets()"...

    Sheesh...things are getting progressively worse around here...

    ---
    William Ernest Reid
     
    Bill Reid, Jun 12, 2008
    #8
  9. Antoninus Twink <> writes:

    > On 12 Jun 2008 at 3:56, fuzhen wrote:
    >> If I want to "press any key to continue" in Windows, I can use getch()
    >> But the getch() isn't a standard C function
    >> So what should I to do in Linux?

    >
    > In most Linux shells, terminal input is usually line-buffered, so your
    > program won't find out about character presses by default until the user
    > presses return. Your main choices are:
    >
    > 1) fiddle with functions in termios.h to switch the terminal to
    > unbuffered mode, then call getchar(), then set the terminal back to
    > buffered mode
    > 2) make a (n)curses application and use getch()
    > 3) settle for "press return to continue" and then do something to read a
    > line, e.g. system("read");


    I wonder if you put the odd suggestions and errors in order to start
    an off-topic debate. As before, post number 3 to comp.unix.programmer
    to find out more about it.

    --
    Ben.
     
    Ben Bacarisse, Jun 12, 2008
    #9
  10. fuzhen

    Guest

    On Jun 12, 5:39 pm, Ben Bacarisse <> wrote:
    > Trollinus Troll writes:
    > > On 12 Jun 2008 at 3:56, fuzhen wrote:
    > >> If I want to "press any key to continue" in Windows, I can use getch()
    > >> But the getch() isn't a standard C function
    > >> So what should I to do in Linux?

    >
    > > In most Linux shells, terminal input is usually line-buffered, so your
    > > program won't find out about character presses by default until the user
    > > presses return. Your main choices are:

    >
    > > 1) fiddle with functions in termios.h to switch the terminal to
    > > unbuffered mode, then call getchar(), then set the terminal back to
    > > buffered mode
    > > 2) make a (n)curses application and use getch()
    > > 3) settle for "press return to continue" and then do something to read a
    > > line, e.g. system("read");

    >
    > I wonder if you put the odd suggestions and errors in order to start
    > an off-topic debate. As before, post number 3 to comp.unix.programmer
    > to find out more about it.

    Wonder? Don't wonder. He's a troll, he was a troll, and he'll always
    be a troll.
    His posts will *always* seek to troll people. It's simple as that, no
    need to complicate it with doubts.
    It's a problem when he replies to newbies with off-topic BS, and
    newbies reply back, usually taking his advice, suggestions etc.
    One option is to always correct the troll. Another would be to flame
    newbies for replying to trolls.
    I prefer the latter, as the former encourages the troll to keep
    posting.
     
    , Jun 12, 2008
    #10
  11. "Bill Reid" <> writes:
    > Keith Thompson <> wrote in message
    > news:...
    >
    >> If you'll settle for requiring the user to press <return> (or <enter>,
    >> or whatever you want to call it), you can have a loop that reads and
    >> discards characters until it sees a '\n' (or EOF).

    >
    > Yes, and you could call that "loop" "gets()" or "fgets()"...
    >
    > Sheesh...things are getting progressively worse around here...


    Neither gets() nor fgets() solves the problem.

    fgets() requires an argument specifying the maximum input length. If
    the input line exceeds that length, fgets() returns before reading the
    new-line; if stdin is line-buffered, additional characters will be
    left on the input stream, waiting to be read by the next input
    operation. It also requires a pointer to storage for a sequence of
    characters that are going to be discarded anyway.

    gets() is inherently dangerous, and should never be used.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jun 12, 2008
    #11
  12. fuzhen

    Bill Reid Guest

    Keith Thompson <> wrote in message
    news:...
    > "Bill Reid" <> writes:
    > > Keith Thompson <> wrote in message
    > > news:...
    > >
    > >> If you'll settle for requiring the user to press <return> (or <enter>,
    > >> or whatever you want to call it), you can have a loop that reads and
    > >> discards characters until it sees a '\n' (or EOF).

    > >
    > > Yes, and you could call that "loop" "gets()" or "fgets()"...
    > >
    > > Sheesh...things are getting progressively worse around here...

    >
    > Neither gets() nor fgets() solves the problem.
    >
    > fgets() requires an argument specifying the maximum input length. If
    > the input line exceeds that length, fgets() returns before reading the
    > new-line; if stdin is line-buffered, additional characters will be
    > left on the input stream, waiting to be read by the next input
    > operation. It also requires a pointer to storage for a sequence of
    > characters that are going to be discarded anyway.
    >
    > gets() is inherently dangerous, and should never be used.


    I said it before and I'll say it again...sheesh...

    Use fgets() with say a 512-byte static buffer you keep in a library just
    for crap like this, and specifically instruct the user to press RETURN,
    and of course you'll still find something to whine about, but in essence
    all you're doing is using the "event loop" of fgets() to break out of the
    loop and "continue", which is what the OP wanted to do, which is simple
    to do, but for some strange reason is IMPOSSIBLE to do here in the
    well-padded confines of this newsgroup...

    I mean, this is just a step ahead (or below!) the "hello world" level
    of programming, but SHEEEEEEESH...it's beyond the newsgroup
    "regulars"...and yes I've said it before, but it is beyond me how anybody
    with that kind of attitude could ever manage to write any useful
    computer code in their life...

    ---
    William Ernest Reid
     
    Bill Reid, Jun 13, 2008
    #12
  13. fuzhen

    Guest

    On Jun 13, 3:34 am, "Bill Reid" <> wrote:
    ....snip...
    >
    > Use fgets() with say a 512-byte static buffer you keep in a library just
    > for crap like this, and specifically instruct the user to press RETURN,

    Spectacular security tips there! (no, not really)
     
    , Jun 13, 2008
    #13
  14. fuzhen

    santosh Guest

    wrote:

    > On Jun 13, 3:34 am, "Bill Reid" <> wrote:
    > ...snip...
    >>
    >> Use fgets() with say a 512-byte static buffer you keep in a library
    >> just for crap like this, and specifically instruct the user to press
    >> RETURN,

    > Spectacular security tips there! (no, not really)


    Remember. Bill has vociferously argued that it's not worth checking
    input for end-of-file conditions.
     
    santosh, Jun 13, 2008
    #14
  15. fuzhen

    Bill Reid Guest

    santosh <> wrote in message
    news:g2sm4r$qua$...
    > wrote:
    > > On Jun 13, 3:34 am, "Bill Reid" <> wrote:


    > > ...snip...
    > >>
    > >> Use fgets() with say a 512-byte static buffer you keep in a library
    > >> just for crap like this, and specifically instruct the user to press
    > >> RETURN,


    > > Spectacular security tips there! (no, not really)

    >
    > Remember. Bill has vociferously argued that it's not worth checking
    > input for end-of-file conditions.


    Nope, I didn't argue "vociferously" about that...what I HAVE said
    is that it NEVER ends with you loons; if I checked for end-of-file, you'd
    find some other apocalyptic event to whine about...

    If only I could stop your incessant trolling as easily as just pressing
    RETURN...

    ---
    William Ernest Reid
     
    Bill Reid, Jun 13, 2008
    #15
  16. fuzhen

    Kevin Handy Guest

    Keith Thompson wrote:
    > Eric Sosman <> writes:
    >> fuzhen wrote:
    >>> If I want to "press any key to continue" in Windows, I can use getch()
    >>> But the getch() isn't a standard C function
    >>> So what should I to do in Linux?

    >> "Press any key other than the one you will press next."
    >>
    >> "Press ENTER" is also a candidate.

    >
    > "Press any key to continue. Press any other key to quit."
    >

    This program broke my computer.!!!!

    I've pressed the caps lock, the shift, the alt, and the control keys.
    Then I tried the num-lock, the print-screen, and the pause buttons.
    Nothing happens! You have totally destroyed my computer!


    ----== Posted via Pronews.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.pronews.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= - Total Privacy via Encryption =---
     
    Kevin Handy, Jun 26, 2008
    #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,464
  2. Matthew
    Replies:
    0
    Views:
    2,907
    Matthew
    Jun 28, 2003
  3. voidstar
    Replies:
    3
    Views:
    10,099
    Mark R Rivet
    Jul 27, 2004
  4. Shugong Wang
    Replies:
    2
    Views:
    9,623
    Raymond Martineau
    Nov 28, 2004
  5. aurgathor
    Replies:
    4
    Views:
    7,931
    Karl Heinz Buchegger
    Mar 16, 2005
Loading...

Share This Page