Flush stdin?

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

    Flush stdin?

    I'm aware that there is no ANSI (or POSIX, or any standard, to my knowledge)
    way of flushing stdin or any other application-level input buffer, but if
    anyone knows a hack or a non-portable way to do this, no matter how obscure
    or arcane, I'd appreciate it.


  • Tom Zych

    #2
    Re: Flush stdin?

    Jonathan Neill wrote:
    [color=blue]
    > I'm aware that there is no ANSI (or POSIX, or any standard, to my knowledge)
    > way of flushing stdin or any other application-level input buffer, but if
    > anyone knows a hack or a non-portable way to do this, no matter how obscure
    > or arcane, I'd appreciate it.[/color]

    Um...what does it mean, to flush an input buffer?

    If you mean you want to read and discard whatever's in the buffer at
    a certain time, under POSIX I suppose you could use non-blocking
    reads until they come up empty. I don't think standard C has
    non-blocking I/O, does it?

    If you mean you want to wipe the buffer's memory space, because the
    user entered a password or something, I'd avoid using stdin for such
    purposes.

    --
    Tom Zych
    This email address will expire at some point to thwart spammers.
    Permanent address: echo 'gbzmlpu@cbobk. pbz' | rot13

    Comment

    • Ricardo Bugalho

      #3
      Re: Flush stdin?


      I agree. In any case, he can try setbuf() to use non-buffered streams.

      --
      Ricardo

      Comment

      • August Derleth

        #4
        Re: Flush stdin?

        "Jonathan Neill" <TYR124840@tyle r.net> wrote in
        news:vlfp829sfd in97@corp.super news.com on Thu 04 Sep 2003 07:16:14p:
        [color=blue]
        > I'm aware that there is no ANSI (or POSIX, or any standard, to my
        > knowledge) way of flushing stdin or any other application-level input
        > buffer, but if anyone knows a hack or a non-portable way to do this, no
        > matter how obscure or arcane, I'd appreciate it.
        >
        >[/color]

        You'd do better asking on a system-specific newsgroup. The geeks there
        will know a dozen ways to do this on your specific system (or will know if
        it can't be done at all). Here, we deal with Standard C, not any system-
        specific extensions to the language. If you have a question about ANSI/ISO
        C (up to and including C99, and even including pre-Standard K&R C if you
        still need it), this is the best place to ask it. If you want to do
        something that is system-specific, ask on a system-specific newsgroup for
        the best answers.

        http://www.eskimo.com/~scs/C-faq/s19.html -- A list of answers to
        questions that can't be answered in Standard C. Read the (rest of the) FAQ
        for more info.

        Comment

        • Richard Heathfield

          #5
          Re: Flush stdin?

          Jonathan Neill wrote:
          [color=blue]
          > I'm aware that there is no ANSI (or POSIX, or any standard, to my
          > knowledge) way of flushing stdin or any other application-level input
          > buffer, but if anyone knows a hack or a non-portable way to do this, no
          > matter how obscure or arcane, I'd appreciate it.[/color]

          What do you mean by "flush"? If you mean that you want to discard the
          current character and all subsequent characters until exactly one \n has
          been discarded (and this is typically what most people want from a
          so-called "flush" of stdin), call this function:

          #include <stdio.h>

          /* constraint: fp must be open for input */
          int fpDrain(FILE *fp)
          {
          int ch = 0;
          while((ch = getc(fp)) != EOF && ch != '\n')
          {
          continue;
          }
          return EOF == ch;
          }

          --
          Richard Heathfield : binary@eton.pow ernet.co.uk
          "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
          C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
          K&R answers, C books, etc: http://users.powernet.co.uk/eton

          Comment

          • Stephan Wilms

            #6
            Re: Flush stdin?

            Hi,

            The problem with non portable solutions is that they tend to be highly
            system specific. The solution to the problem must be tailored to the
            specifics of the systems where it is intended to be used. Since you
            did not describe your system / environment we can not give you a
            specific working solution.

            For instance there are systems where "fflush(stdin); " actually works
            and does what you want (e.g. Microsoft Visual C). Another solution
            would indeed be to check your system for a non blocking function to
            read from a stream or terminal. To add to the already existant obscure
            examples: Microsoft C offers a function called "_kbhit()" to check if
            there is pending keyboard input available.

            --
            Stephan Wilms

            "Jonathan Neill" <TYR124840@tyle r.net> wrote in message news:<vlfp829sf din97@corp.supe rnews.com>...[color=blue]
            > I'm aware that there is no ANSI (or POSIX, or any standard, to my knowledge)
            > way of flushing stdin or any other application-level input buffer, but if
            > anyone knows a hack or a non-portable way to do this, no matter how obscure
            > or arcane, I'd appreciate it.[/color]

            Comment

            • Martin Ambuhl

              #7
              Re: Flush stdin?

              Jonathan Neill wrote:
              [color=blue]
              > I'm aware that there is no ANSI (or POSIX, or any standard, to my knowledge)
              > way of flushing stdin or any other application-level input buffer, but if
              > anyone knows a hack or a non-portable way to do this, no matter how obscure
              > or arcane, I'd appreciate it.[/color]

              It doesn't even mean anything. Read the description of the action of
              fflush() on output streams and try to come up with a reasonable analogy for
              input streams.



              --
              Martin Ambuhl

              Comment

              • Alex Vinokur

                #8
                Re: Flush stdin?


                "Jonathan Neill" <TYR124840@tyle r.net> wrote in message news:vlfp829sfd in97@corp.super news.com...[color=blue]
                > I'm aware that there is no ANSI (or POSIX, or any standard, to my knowledge)
                > way of flushing stdin or any other application-level input buffer, but if
                > anyone knows a hack or a non-portable way to do this, no matter how obscure
                > or arcane, I'd appreciate it.
                >
                >[/color]
                Look at :

                http://alexvn.freeservers.com/s1/download.html (Flushing non-read input data with using tcflush)


                =============== =============== =======
                Alex Vinokur
                mailto:alexvn@c onnect.to

                =============== =============== =======


                Comment

                • Richard Heathfield

                  #9
                  Re: Flush stdin?

                  Jonathan Neill wrote:
                  [color=blue]
                  > I'm aware that there is no ANSI (or POSIX, or any standard, to my
                  > knowledge) way of flushing stdin or any other application-level input
                  > buffer, but if anyone knows a hack or a non-portable way to do this, no
                  > matter how obscure or arcane, I'd appreciate it.[/color]

                  What do you mean by "flush"? If you mean that you want to discard the
                  current character and all subsequent characters until exactly one \n has
                  been discarded (and this is typically what most people want from a
                  so-called "flush" of stdin), call this function:

                  #include <stdio.h>

                  /* constraint: fp must be open for input */
                  int fpDrain(FILE *fp)
                  {
                  int ch = 0;
                  while((ch = getc(fp)) != EOF && ch != '\n')
                  {
                  continue;
                  }
                  return EOF == ch;
                  }

                  --
                  Richard Heathfield : binary@eton.pow ernet.co.uk
                  "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
                  C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
                  K&R answers, C books, etc: http://users.powernet.co.uk/eton

                  Comment

                  • Stephan Wilms

                    #10
                    Re: Flush stdin?

                    Hi,

                    The problem with non portable solutions is that they tend to be highly
                    system specific. The solution to the problem must be tailored to the
                    specifics of the systems where it is intended to be used. Since you
                    did not describe your system / environment we can not give you a
                    specific working solution.

                    For instance there are systems where "fflush(stdin); " actually works
                    and does what you want (e.g. Microsoft Visual C). Another solution
                    would indeed be to check your system for a non blocking function to
                    read from a stream or terminal. To add to the already existant obscure
                    examples: Microsoft C offers a function called "_kbhit()" to check if
                    there is pending keyboard input available.

                    --
                    Stephan Wilms

                    "Jonathan Neill" <TYR124840@tyle r.net> wrote in message news:<vlfp829sf din97@corp.supe rnews.com>...[color=blue]
                    > I'm aware that there is no ANSI (or POSIX, or any standard, to my knowledge)
                    > way of flushing stdin or any other application-level input buffer, but if
                    > anyone knows a hack or a non-portable way to do this, no matter how obscure
                    > or arcane, I'd appreciate it.[/color]

                    Comment

                    • Martin Ambuhl

                      #11
                      Re: Flush stdin?

                      Jonathan Neill wrote:
                      [color=blue]
                      > I'm aware that there is no ANSI (or POSIX, or any standard, to my knowledge)
                      > way of flushing stdin or any other application-level input buffer, but if
                      > anyone knows a hack or a non-portable way to do this, no matter how obscure
                      > or arcane, I'd appreciate it.[/color]

                      It doesn't even mean anything. Read the description of the action of
                      fflush() on output streams and try to come up with a reasonable analogy for
                      input streams.



                      --
                      Martin Ambuhl

                      Comment

                      • Tom Zych

                        #12
                        Re: Flush stdin?

                        Stephan Wilms wrote:
                        [color=blue]
                        > The problem with non portable solutions is that they tend to be highly
                        > system specific.[/color]

                        <captious>

                        Really? Gosh. I guess that's why they're non-portable, then?

                        </captious>

                        --
                        Tom Zych
                        This email address will expire at some point to thwart spammers.
                        Permanent address: echo 'gbzmlpu@cbobk. pbz' | rot13

                        Comment

                        • Alex Vinokur

                          #13
                          Re: Flush stdin?


                          "Jonathan Neill" <TYR124840@tyle r.net> wrote in message news:vlfp829sfd in97@corp.super news.com...[color=blue]
                          > I'm aware that there is no ANSI (or POSIX, or any standard, to my knowledge)
                          > way of flushing stdin or any other application-level input buffer, but if
                          > anyone knows a hack or a non-portable way to do this, no matter how obscure
                          > or arcane, I'd appreciate it.
                          >
                          >[/color]
                          Look at :

                          http://alexvn.freeservers.com/s1/download.html (Flushing non-read input data with using tcflush)


                          =============== =============== =======
                          Alex Vinokur
                          mailto:alexvn@c onnect.to

                          =============== =============== =======


                          Comment

                          • Tom Zych

                            #14
                            Re: Flush stdin?

                            Stephan Wilms wrote:
                            [color=blue]
                            > The problem with non portable solutions is that they tend to be highly
                            > system specific.[/color]

                            <captious>

                            Really? Gosh. I guess that's why they're non-portable, then?

                            </captious>

                            --
                            Tom Zych
                            This email address will expire at some point to thwart spammers.
                            Permanent address: echo 'gbzmlpu@cbobk. pbz' | rot13

                            Comment

                            • The Real OS/2 Guy

                              #15
                              Re: Flush stdin?

                              On Fri, 5 Sep 2003 01:16:14 UTC, "Jonathan Neill"
                              <TYR124840@tyle r.net> wrote:
                              [color=blue]
                              > I'm aware that there is no ANSI (or POSIX, or any standard, to my knowledge)
                              > way of flushing stdin or any other application-level input buffer, but if
                              > anyone knows a hack or a non-portable way to do this, no matter how obscure
                              > or arcane, I'd appreciate it.[/color]

                              100% ANSI compatible:

                              /* flush stdin */
                              /* params: */
                              /* int c int to hold temorary readed char from stdin */
                              /* */
                              /* read from stdin until end of file or end of line reached */
                              #define FLUSHIN(c) while ((c = getc() != EOF && c != '\n')

                              stdin is commonly line buffered or unbuffered. In both cases reading
                              until '\n' is dedected is flushing the input stream. Even when stdin
                              is set to be block buffered (e.g. as pipe or file it makes sense to
                              "flush". Because stdin is a text stram, so reading until '\n'
                              syncronises the stream so that the next coming char will be the first
                              of a line.

                              --
                              Tschau/Bye
                              Herbert

                              eComStation 1.1 Deutsch Beta ist verügbar

                              Comment

                              Working...