scanf behaviour

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

    #31
    Re: scanf behaviour

    Mark McIntyre wrote:
    On 24 Nov 2006 23:29:46 -0800, in comp.lang.c , stasgold@gmail. com
    wrote:
    >
    >>I've tested "fflush( stdin );" on both WinXP and SunOS boxes it works
    >>without a glitch.
    >
    That means nothing. It may work only in debug mode, or only provided
    there is no other file operation going on in another application, or
    only on saturdays. Tomorrow, when you're doing a demonstration to your
    boss, it may fail.
    >
    I don't know about XP, but on Solaris fflush() on an input stream is
    defined:

    "Flushing an input stream discards any buffered input and
    adjusts the file pointer such that the next input operation
    accesses the byte after the last one read."

    This non-standard behaviour probably goes back to pre-standard days and
    should be avoided in portable code.

    --
    Ian Collins.

    Comment

    • Mark McIntyre

      #32
      Re: scanf behaviour

      On Sun, 26 Nov 2006 12:04:47 +1300, in comp.lang.c , Ian Collins
      <ian-news@hotmail.co mwrote:
      >But they suck it from the barrel and flushing clears the pipes that feed
      >the pump, which makes them the pump's stdin :)
      It may surprise you to learn this, given the quality of most beer, but
      you do *not* flush the pipecleaner out into the barrel.....

      --
      Mark McIntyre

      "Debugging is twice as hard as writing the code in the first place.
      Therefore, if you write the code as cleverly as possible, you are,
      by definition, not smart enough to debug it."
      --Brian Kernighan

      Comment

      • Ian Collins

        #33
        Re: scanf behaviour

        Mark McIntyre wrote:
        On Sun, 26 Nov 2006 12:04:47 +1300, in comp.lang.c , Ian Collins
        <ian-news@hotmail.co mwrote:
        >
        >>But they suck it from the barrel and flushing clears the pipes that feed
        >>the pump, which makes them the pump's stdin :)
        >
        It may surprise you to learn this, given the quality of most beer, but
        you do *not* flush the pipecleaner out into the barrel.....
        >
        I'm not talking about cleaning the pipes, but pulling though to flush
        stale beer before serving the first pint of the session, the stretched
        analogy being discarding the stale characters from a stream.

        --
        Ian Collins.

        Comment

        • Richard Heathfield

          #34
          Re: scanf behaviour

          Ian Collins said:
          Mark McIntyre wrote:
          >On Sun, 26 Nov 2006 12:04:47 +1300, in comp.lang.c , Ian Collins
          ><ian-news@hotmail.co mwrote:
          >>
          >>>But they suck it from the barrel and flushing clears the pipes that feed
          >>>the pump, which makes them the pump's stdin :)
          >>
          >It may surprise you to learn this, given the quality of most beer, but
          >you do *not* flush the pipecleaner out into the barrel.....
          >>
          I'm not talking about cleaning the pipes, but pulling though to flush
          stale beer before serving the first pint of the session, the stretched
          analogy being discarding the stale characters from a stream.
          If you insist on pushing this analogy, it's actually equivalent to something
          that you might find in start-up code, such as:

          memset(_iobuf._ bufdata, 0, sizeof _iobuf._bufdata );
          _iobuf[_STDOUT]._bufpos=0;

          i.e. making sure that no stale characters remain in the output buffer, where
          they might get served to a file by mistake.

          But in any case it's a lousy analogy.

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

          email: rjh at the above domain, - www.

          Comment

          • Richard Heathfield

            #35
            Re: scanf behaviour

            Ian Collins said:
            stasgold@gmail. com wrote:
            >Thanks for the reference
            >>
            >By the way, the usage of "fflush( stdin ) is quite common
            >
            Probably only on systems where the behaviour is defined and predates
            standard C.
            Do you know of any such systems? Note that C was standardised in 1989.

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

            email: rjh at the above domain, - www.

            Comment

            • Richard Heathfield

              #36
              Re: scanf behaviour

              Ian Collins said:

              <snip>
              I don't know about XP, but on Solaris fflush() on an input stream is
              defined:
              But in C, it isn't, and we discuss C here, not Solaris.
              This non-standard behaviour probably goes back to pre-standard days
              Unlikely. Do you have evidence to support that suggestion?
              and should be avoided in portable code.
              Agreed.

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

              email: rjh at the above domain, - www.

              Comment

              • Ian Collins

                #37
                Re: scanf behaviour

                Richard Heathfield wrote:
                Ian Collins said:
                >
                <snip>
                >
                >>This non-standard behaviour probably goes back to pre-standard days
                >
                Unlikely. Do you have evidence to support that suggestion?
                >
                I might have, but it would be off topic.

                --
                Ian Collins.

                Comment

                • Ian Collins

                  #38
                  Re: scanf behaviour

                  Richard Heathfield wrote:
                  Ian Collins said:
                  >
                  >>stasgold@gmai l.com wrote:
                  >>
                  >>>Thanks for the reference
                  >>>
                  >>>By the way, the usage of "fflush( stdin ) is quite common
                  >>
                  >>Probably only on systems where the behaviour is defined and predates
                  >>standard C.
                  >
                  Do you know of any such systems? Note that C was standardised in 1989.
                  >
                  SunOS

                  --
                  Ian Collins.

                  Comment

                  • Richard Heathfield

                    #39
                    Re: scanf behaviour

                    Ian Collins said:
                    Richard Heathfield wrote:
                    >Ian Collins said:
                    >>
                    >>>stasgold@gma il.com wrote:
                    >>>
                    >>>>Thanks for the reference
                    >>>>
                    >>>>By the way, the usage of "fflush( stdin ) is quite common
                    >>>
                    >>>Probably only on systems where the behaviour is defined and predates
                    >>>standard C.
                    >>
                    >Do you know of any such systems? Note that C was standardised in 1989.
                    >>
                    SunOS
                    I can't find any evidence to support your assertion. Can you?

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

                    email: rjh at the above domain, - www.

                    Comment

                    • Ian Collins

                      #40
                      Re: scanf behaviour

                      Richard Heathfield wrote:
                      Ian Collins said:
                      >
                      >
                      >>Richard Heathfield wrote:
                      >>
                      >>>Ian Collins said:
                      >>>
                      >>>
                      >>>>stasgold@gm ail.com wrote:
                      >>>>
                      >>>>
                      >>>>>Thanks for the reference
                      >>>>>
                      >>>>>By the way, the usage of "fflush( stdin ) is quite common
                      >>>>
                      >>>>Probably only on systems where the behaviour is defined and predates
                      >>>>standard C.
                      >>>
                      >>>Do you know of any such systems? Note that C was standardised in 1989.
                      >>>
                      >>
                      >>SunOS
                      >
                      I can't find any evidence to support your assertion. Can you?
                      >
                      It wasn't an assertion, just speculation as to why an unmentionable
                      operating system defined fflush() on an input stream.

                      SunOS 4 predates ANSI C and it supported the fflush() function. I've
                      lost my old man pages, so I can't prove the documented behaviour.

                      --
                      Ian Collins.

                      Comment

                      • mdbenchrifa@gmail.com

                        #41
                        Re: scanf behaviour


                        stasgold@gmail. com a écrit :
                        Hello.
                        >
                        I maybe reinvent the weel ...
                        I'm trying to read positive integer number with the help of scanf, if
                        the input value is not positive number but negaive one zero or char , i
                        have to reread the input until I get the needed pos. number
                        >
                        I wrote the code , but it doesn't work the way it should : if i put
                        some char into input, the program goes infinite loop instead of
                        promting me to enter a new value.
                        >
                        Please shed some light on what is the problem. thanks
                        Code:
                        #include <stdio.h>
                        >
                        int main()
                        {
                        	int n;
                        	while (1)
                        	{
                        		printf("Please enter positive number \n");
                        		if ( scanf("%d",&n)!=1 || n<=0 )
                        		{
                        			printf("Illegal input!\n");
                        			continue;
                        		}
                        >
                        	return 0;
                        	}
                        return 1;
                        }
                        Just add gets(str) like
                        int main()
                        {
                        int n;
                        char str[10];
                        while (1)
                        {
                        printf("Please enter positive number \n");
                        scanf("%d",&n);
                        if ( n<=0 )
                        {
                        printf("Illegal input!\n");
                        gets(str);
                        continue;
                        }


                        return 0;
                        }

                        system("PAUSE") ;
                        return 1;
                        }

                        Comment

                        • Richard Heathfield

                          #42
                          Re: scanf behaviour

                          mdbenchrifa@gma il.com said:

                          <snip>
                          Just add gets(str)
                          While you're at it, drive to the hottest crime-spot you can find, wind down
                          the driver's window of your car, leave the keys in the ignition, and go for
                          a six-hour walk.

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

                          email: rjh at the above domain, - www.

                          Comment

                          • Harald van Dijk

                            #43
                            Re: scanf behaviour

                            mdbenchrifa@gma il.com wrote:
                            stasgold@gmail. com a écrit :
                            [...]
                            I wrote the code , but it doesn't work the way it should : if i put
                            some char into input, the program goes infinite loop instead of
                            promting me to enter a new value.

                            Please shed some light on what is the problem. thanks
                            [code]
                            #include <stdio.h>

                            int main()
                            {
                            int n;
                            while (1)
                            {
                            printf("Please enter positive number \n");
                            if ( scanf("%d",&n)! =1 || n<=0 )
                            {
                            printf("Illegal input!\n");
                            continue;
                            }
                            [...]
                            Just add gets(str) like
                            Please take a look at http://c-faq.com/stdio/getsvsfgets.htm l

                            Comment

                            • Dik T. Winter

                              #44
                              Re: scanf behaviour

                              In article <8KSdnQ7x15-Eg_TYnZ2dnUVZ8s udnZ2d@bt.comrjh@see.sig.inv alid writes:
                              Ian Collins said:
                              I don't know about XP, but on Solaris fflush() on an input stream is
                              defined:
                              >
                              But in C, it isn't, and we discuss C here, not Solaris.
                              It is also not on Solaris, at least not on the version I am using
                              (SunOS 5.8). Can Ian Collins can tell me were it is defined. It
                              is not in the man page for fflush on the system I use.
                              --
                              dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
                              home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/

                              Comment

                              • Ian Collins

                                #45
                                Re: scanf behaviour

                                Dik T. Winter wrote:
                                In article <8KSdnQ7x15-Eg_TYnZ2dnUVZ8s udnZ2d@bt.comrjh@see.sig.inv alid writes:
                                Ian Collins said:
                                I don't know about XP, but on Solaris fflush() on an input stream is
                                defined:
                                >
                                But in C, it isn't, and we discuss C here, not Solaris.
                                >
                                It is also not on Solaris, at least not on the version I am using
                                (SunOS 5.8). Can Ian Collins can tell me were it is defined. It
                                is not in the man page for fflush on the system I use.
                                From 5.9 onwards.

                                --
                                Ian Collins.

                                Comment

                                Working...