function system() from stdlib.h

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

    function system() from stdlib.h

    Hi,

    I had write a program for Windows OS and put the following line:

    system("pause")

    that use the "pause" command from DOS to pause the screen until I push
    a button. How to get the same result in Linux's bash ?

    thanks a lot

  • Zack

    #2
    Re: function system() from stdlib.h

    On Feb 15, 4:48 pm, "britzkrieg " <eap.blitzkr... @gmail.comwrote :
    pause the screen until I push a button.


    Comment

    • santosh

      #3
      Re: function system() from stdlib.h

      britzkrieg wrote:
      Hi,
      >
      I had write a program for Windows OS and put the following line:
      >
      system("pause")
      >
      that use the "pause" command from DOS to pause the screen until I push
      a button. How to get the same result in Linux's bash ?
      Why not use getchar() and discard the return value? It's more portable
      than executing OS specific commands.

      Comment

      • Chris Dollin

        #4
        Re: function system() from stdlib.h

        britzkrieg wrote:
        Hi,
        >
        I had write a program for Windows OS and put the following line:
        >
        system("pause")
        >
        that use the "pause" command from DOS to pause the screen until I push
        a button. How to get the same result in Linux's bash ?
        Well, what /I'd/ do is do

        program | more

        and not bother doing anything in the code. But if you really want
        to wait for a character, why not call:

        void waitForRETURN(v oid)
        {
        fprintf( stderr, "press RETURN to continue:" );
        fflush( stderr );
        while (getchar() != '\n') {}
        }

        Pickier people might want to handle EOF. Prettier people might
        want to parametrise with a reason message. Perfectionist people
        will probably see five things wrong I won't notice until after
        I post, or after my coffee.

        --
        Chris "problemati c hedgehog" Dollin
        "The path to the web becomes deeper and wider" - October Project

        Comment

        • santosh

          #5
          Re: function system() from stdlib.h


          Chris Dollin wrote:
          britzkrieg wrote:
          >
          Hi,

          I had write a program for Windows OS and put the following line:

          system("pause")

          that use the "pause" command from DOS to pause the screen until I push
          a button. How to get the same result in Linux's bash ?
          >
          Well, what /I'd/ do is do
          >
          program | more
          >
          and not bother doing anything in the code. But if you really want
          to wait for a character, why not call:
          >
          void waitForRETURN(v oid)
          {
          fprintf( stderr, "press RETURN to continue:" );
          fflush( stderr );
          while (getchar() != '\n') {}
          }
          Is there a specific reason for doing while( ... ) {} instead of
          while( ... ); other than readability? I consider the latter form to be
          more readable, but I may be in the minority regarding that.
          Pickier people might want to handle EOF. Prettier people might
          want to parametrise with a reason message. Perfectionist people
          will probably see five things wrong I won't notice until after
          I post, or after my coffee.
          What if stdin is already at end-of-file, when getchar() is invoked?

          Comment

          • Chris Dollin

            #6
            Re: function system() from stdlib.h

            santosh wrote:
            Chris Dollin wrote:
            >>
            > void waitForRETURN(v oid)
            > {
            > fprintf( stderr, "press RETURN to continue:" );
            > fflush( stderr );
            > while (getchar() != '\n') {}
            > }
            >
            Is there a specific reason for doing while( ... ) {} instead of
            while( ... ); other than readability? I consider the latter form to be
            more readable, but I may be in the minority regarding that.
            Readability. I find `;` to be more missable (and more likely to
            be an accident) than {}. One's parsecage may vary.
            >Pickier people might want to handle EOF. Prettier people might
            >want to parametrise with a reason message. Perfectionist people
            >will probably see five things wrong I won't notice until after
            >I post, or after my coffee.
            >
            What if stdin is already at end-of-file, when getchar() is invoked?
            "Pickier people might want to handle EOF."

            --
            Chris "electric hedgehog" Dollin
            "How am I to understand if you won't teach me?" - Trippa, /Falling/

            Comment

            • =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=

              #7
              Re: function system() from stdlib.h

              santosh wrote:
              Chris Dollin wrote:
              [...]
              void waitForRETURN(v oid)
              {
              fprintf( stderr, "press RETURN to continue:" );
              fflush( stderr );
              while (getchar() != '\n') {}
              }
              >
              Is there a specific reason for doing while( ... ) {} instead of
              while( ... ); other than readability? I consider the latter form to be
              more readable, but I may be in the minority regarding that.
              There are no benefits other than readability. I'm in the group that
              considers {} more readable where a statement without effect is
              required. Better yet to me would be a comment, with either form.

              while (getchar() != '\n')
              /* do nothing */ ;

              while (getchar() != '\n')
              { /* do nothing */ }
              Pickier people might want to handle EOF. Prettier people might
              want to parametrise with a reason message. Perfectionist people
              will probably see five things wrong I won't notice until after
              I post, or after my coffee.
              >
              What if stdin is already at end-of-file, when getchar() is invoked?
              Then it may loop forever comparing EOF to '\n' and doing nothing. It's
              also possible that EOF is reached during the loop, since input is not
              necessarily terminated by a newline character.

              Comment

              • Richard Heathfield

                #8
                Re: function system() from stdlib.h

                santosh said:
                >
                Chris Dollin wrote:
                <snip>
                >>
                > void waitForRETURN(v oid)
                > {
                > fprintf( stderr, "press RETURN to continue:" );
                > fflush( stderr );
                > while (getchar() != '\n') {}
                > }
                >
                Is there a specific reason for doing while( ... ) {} instead of
                while( ... ); other than readability? I consider the latter form to be
                more readable, but I may be in the minority regarding that.
                Obviously both forms are correct; it's a style issue. Personally, I
                prefer

                while(...)
                {
                continue;
                }

                which is even more verbose. But Chris's version is fine and so is yours.
                Some compilers, however, will issue a diagnostic message for your
                version, not because it is required but because it is helpful to
                newbies who plaster semicolons all over their code in sheer
                desperation, to not-quite-newbies who aren't completely sure of their
                syntax rules, and even to oldbies - we all tismype occasionally.
                >Pickier people might want to handle EOF. [...]
                >
                What if stdin is already at end-of-file, when getchar() is invoked?
                That makes you a pickier people (which is good, and it's why Chris
                specifically covered his back with all those spiky bits).

                --
                Richard Heathfield
                "Usenet is a strange place" - dmr 29/7/1999

                email: rjh at the above domain, - www.

                Comment

                • santosh

                  #9
                  Re: function system() from stdlib.h

                  Richard Heathfield wrote:
                  santosh said:
                  Chris Dollin wrote:
                  >
                  <snip>
                  >
                  void waitForRETURN(v oid)
                  {
                  fprintf( stderr, "press RETURN to continue:" );
                  fflush( stderr );
                  while (getchar() != '\n') {}
                  }
                  Is there a specific reason for doing while( ... ) {} instead of
                  while( ... ); other than readability? I consider the latter form to be
                  more readable, but I may be in the minority regarding that.
                  >
                  Obviously both forms are correct; it's a style issue. Personally, I
                  prefer
                  >
                  while(...)
                  {
                  continue;
                  }
                  >
                  which is even more verbose. But Chris's version is fine and so is yours.
                  Some compilers, however, will issue a diagnostic message for your
                  version, not because it is required but because it is helpful to
                  newbies who plaster semicolons all over their code in sheer
                  desperation, to not-quite-newbies who aren't completely sure of their
                  syntax rules, and even to oldbies - we all tismype occasionally.
                  Out of curiosity, can you name a compiler that emits this diagnostic?
                  I haven't encountered it in gcc, even with the -Wall and -Wextra
                  switches.

                  Comment

                  • Richard Heathfield

                    #10
                    Re: function system() from stdlib.h

                    santosh said:

                    <snip>
                    >
                    Out of curiosity, can you name a compiler that emits this diagnostic?
                    [for the while(condition ); construct]

                    Not off the top of my head, no.

                    I just tested it with gcc, and it doesn't seem to do this by default, by
                    which I mean my usual plethora of bizarre gcc switches.

                    I could test with Borland and Microsoft, but that would involve effort.
                    :-)

                    Maybe it was some lint or other (shrug), or maybe I'm just
                    disremembering. If so, then I am also disremembering that one way to
                    quell the warning was to add a space: while(condition ) ;

                    --
                    Richard Heathfield
                    "Usenet is a strange place" - dmr 29/7/1999

                    email: rjh at the above domain, - www.

                    Comment

                    • Christopher Layne

                      #11
                      Re: function system() from stdlib.h

                      britzkrieg wrote:
                      Hi,
                      >
                      I had write a program for Windows OS and put the following line:
                      >
                      system("pause")
                      >
                      that use the "pause" command from DOS to pause the screen until I push
                      a button. How to get the same result in Linux's bash ?
                      >
                      thanks a lot
                      "read"

                      Literally:

                      #!/bin/bash
                      read -p "press enter key when ready"
                      <whatever else>

                      Now if it's a modern version of bash and you're commited to being bash
                      specific:

                      read -s -n 1 -p "press any key when ready"

                      Silent echo, 1 char max

                      Comment

                      • Christopher Layne

                        #12
                        Re: function system() from stdlib.h

                        Christopher Layne wrote:
                        >that use the "pause" command from DOS to pause the screen until I push
                        >a button. How to get the same result in Linux's bash ?
                        >>
                        >thanks a lot
                        >
                        "read"
                        >
                        Literally:
                        Also my answer was jsut in general for bash scripts. IF you're trying to
                        attain the same result on both DOS/Unix platforms, follow the advice already
                        presented. Definitely do not use system() for this.

                        Comment

                        • britzkrieg

                          #13
                          Re: function system() from stdlib.h

                          Christopher Layne wrote:
                          #!/bin/bash
                          read -p "press enter key when ready"
                          <whatever else>
                          >
                          Now if it's a modern version of bash and you're commited to being bash
                          specific:
                          >
                          read -s -n 1 -p "press any key when ready"
                          >
                          Silent echo, 1 char max
                          I haven't test the Christopher's solution yet. But the getchar()
                          solution isn't work; I dont' know what's going on, but my program get
                          off without I push a button.

                          Comment

                          • Chris Dollin

                            #14
                            Re: function system() from stdlib.h

                            britzkrieg wrote:
                            I haven't test the Christopher's solution yet. But the getchar()
                            solution isn't work; I dont' know what's going on, but my program get
                            off without I push a button.
                            Well, we can't see your code, so we can't see what you've done
                            wrong.

                            But I /guess/ that you did some input earlier and didn't
                            eat all of it, so your getchar getted a char without you
                            having to type any more. Solution: don't leave random input
                            unread. (I have a small bet with my evil twin that it's your
                            `scanf` that's the root of the problem.)

                            --
                            Chris "fgets!" Dollin
                            The "good old days" used to be much better.

                            Comment

                            • CBFalconer

                              #15
                              Re: function system() from stdlib.h

                              Chris Dollin wrote:
                              santosh wrote:
                              >
                              .... snip ...
                              >>
                              >Is there a specific reason for doing while( ... ) {} instead of
                              >while( ... ); other than readability? I consider the latter form
                              >to be more readable, but I may be in the minority regarding that.
                              >
                              Readability. I find `;` to be more missable (and more likely to
                              be an accident) than {}. One's parsecage may vary.
                              Try:

                              while (side_effects) continue;

                              --
                              <http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
                              <http://www.securityfoc us.com/columnists/423>

                              "A man who is right every time is not likely to do very much."
                              -- Francis Crick, co-discover of DNA
                              "There is nothing more amazing than stupidity in action."
                              -- Thomas Matthews


                              Comment

                              Working...