about getch()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fuzhen

    about getch()

    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?
  • Eric Sosman

    #2
    Re: about getch()

    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.

    --
    Eric Sosman
    esosman@ieee-dot-org.invalid

    Comment

    • rahul

      #3
      Re: about getch()

      On Jun 12, 8:56 am, fuzhen <fuc...@gmail.c omwrote:
      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.

      Comment

      • Peter Nilsson

        #4
        Re: about getch()

        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

        Comment

        • Keith Thompson

          #5
          Re: about getch()

          Eric Sosman <esosman@ieee-dot-org.invalidwrit es:
          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_Keit h) kst-u@mib.org <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"

          Comment

          • Keith Thompson

            #6
            Re: about getch()

            fuzhen <fucore@gmail.c omwrites:
            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_Keit h) kst-u@mib.org <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"

            Comment

            • CBFalconer

              #7
              Re: about getch()

              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.

              Try "Press 'return' to continue" followed by "fflush(stdin); " and
              "getchar(); ". You can substitute 'Enter' for 'return' if you
              wish. You can also substitute skipln(stdin) for getchar() (which
              will be cleaner), where:

              int skipln(FILE *f) {
              int ch;

              while ((EOF != (ch = getc(f))) && ('\n' != ch)) continue;
              return ch;
              } /* skipln */

              --
              [mail]: Chuck F (cbfalconer at maineline dot net)
              [page]: <http://cbfalconer.home .att.net>
              Try the download section.


              ** Posted from http://www.teranews.com **

              Comment

              • Mark McIntyre

                #8
                Re: about getch()

                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?

                Comment

                • Antoninus Twink

                  #9
                  Re: about getch()

                  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");

                  Comment

                  • vippstar@gmail.com

                    #10
                    Re: about getch()

                    On Jun 12, 10:49 am, Mark McIntyre <markmcint...@T ROUSERSspamcop. net>
                    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)

                    Comment

                    • Bill Reid

                      #11
                      Re: about getch()


                      Keith Thompson <kst-u@mib.orgwrote in message
                      news:lnzlpri3yo .fsf@nuthaus.mi b.org...
                      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



                      Comment

                      • Keith Thompson

                        #12
                        Re: about getch()

                        "Bill Reid" <hormelfree@hap pyhealthy.netwr ites:
                        Keith Thompson <kst-u@mib.orgwrote in message
                        news:lnzlpri3yo .fsf@nuthaus.mi b.org...
                        >
                        >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_Keit h) kst-u@mib.org <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"

                        Comment

                        • Bill Reid

                          #13
                          Re: about getch()


                          Keith Thompson <kst-u@mib.orgwrote in message
                          news:lnve0eirqh .fsf@nuthaus.mi b.org...
                          "Bill Reid" <hormelfree@hap pyhealthy.netwr ites:
                          Keith Thompson <kst-u@mib.orgwrote in message
                          news:lnzlpri3yo .fsf@nuthaus.mi b.org...
                          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...i t's beyond the newsgroup
                          "regulars"...an d 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



                          Comment

                          • vippstar@gmail.com

                            #14
                            Re: about getch()

                            On Jun 13, 3:34 am, "Bill Reid" <hormelf...@hap pyhealthy.netwr ote:
                            ....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)

                            Comment

                            • santosh

                              #15
                              Re: about getch()

                              vippstar@gmail. com wrote:
                              On Jun 13, 3:34 am, "Bill Reid" <hormelf...@hap pyhealthy.netwr ote:
                              ...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.

                              Comment

                              Working...