How to sscanf return integer only

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

    How to sscanf return integer only

    I have the following code:

    sscanf(line, "%d", n_ptr) !=1 || n_ptr <=0;

    It only partially works. If the user types a character other than 0-9 to
    start the string it fails. However as long as the first character is an
    integer it will allow letters in the following places

    for Example:

    H78 (fails as expected)
    78 (works as expected)
    7H8 ( doesn't fail as desired)






  • Jordan Abel

    #2
    Re: How to sscanf return integer only

    On 2006-05-05, Yogi_Bear_79 <nospam@spamsux .com> wrote:[color=blue]
    > I have the following code:
    >
    > sscanf(line, "%d", n_ptr) !=1 || n_ptr <=0;
    >
    > It only partially works. If the user types a character other than 0-9 to
    > start the string it fails. However as long as the first character is an
    > integer it will allow letters in the following places
    >
    > for Example:
    >
    > H78 (fails as expected)
    > 78 (works as expected)
    > 7H8 ( doesn't fail as desired)[/color]

    But there _is_ an integer - 7. there's then stuff after the integer.

    and what's "|| n_ptr <=0" supposed to do?

    Comment

    • Yogi_Bear_79

      #3
      Re: How to sscanf return integer only

      [color=blue]
      > But there _is_ an integer - 7. there's then stuff after the integer.[/color]

      It needs to fail if there are any characters other than integers or white
      space

      [color=blue]
      > and what's "|| n_ptr <=0" supposed to do?[/color]

      Not needed, removed, same results


      Comment

      • Barry Schwarz

        #4
        Re: How to sscanf return integer only

        On Thu, 4 May 2006 20:23:56 -0400, "Yogi_Bear_ 79" <nospam@spamsux .com>
        wrote:
        [color=blue]
        >I have the following code:
        >
        >sscanf(line, "%d", n_ptr) !=1 || n_ptr <=0;[/color]

        The third argument needs an & but I assume that is a typo.

        If you want to know whether the data at line is a "well formed"
        integer, use strtol which will tell you where it stopped extracting
        data. Then examine that char for white space or '\0'.
        [color=blue]
        >
        >It only partially works. If the user types a character other than 0-9 to
        >start the string it fails. However as long as the first character is an
        >integer it will allow letters in the following places
        >
        >for Example:
        >
        >H78 (fails as expected)
        >78 (works as expected)
        >7H8 ( doesn't fail as desired)
        >
        >
        >
        >
        >[/color]


        Remove del for email

        Comment

        • Robert Gamble

          #5
          Re: How to sscanf return integer only

          Yogi_Bear_79 wrote:[color=blue][color=green]
          > > But there _is_ an integer - 7. there's then stuff after the integer.[/color]
          >
          > It needs to fail if there are any characters other than integers or white
          > space
          >
          >[color=green]
          > > and what's "|| n_ptr <=0" supposed to do?[/color]
          >
          > Not needed, removed, same results[/color]

          sscanf isn't the best tool for the job for a couple of reasons, not the
          least of which is that trying to convert a number of too large a
          magnitude results in undefined behavior.

          The following program will read a line at a time from stdin and attempt
          to convert it to an integer using the strtol function. If the line
          contains any invalid characters, an error message is displayed and the
          offending character is pointed out. If a range error occurs (the
          number was too large/small to store in a long int), an appropriate
          message is diaplayed. This version uses 'long int' and accepts
          negative numbers, remove the minus sign from the string of allowed
          characters if you just want to accept positive numbers. The code is a
          little verbose since it implements error checking.

          #include <stdio.h>
          #include <string.h>
          #include <stdlib.h>
          #include <errno.h>
          #define BUF_MAX 80

          int main (void) {
          char buf[BUF_MAX];
          long l;
          size_t s;
          while(fgets(buf , BUF_MAX, stdin) != NULL) {
          if ((s = strspn(buf, "-0123456789 \t\n")) == strlen(buf)) {
          errno = 0;
          l = strtol(buf, NULL, 10);
          if (errno == ERANGE) {
          printf("number out of range!\n");
          } else {
          printf("number read was %ld\n", l);
          }
          } else {
          while(s--)
          putchar(' ');
          putchar('^');
          printf(" invalid character\n");
          }
          }
          return 0;
          }

          The strspn function returns the number of characters in the input
          string that are in the "allow" string, this must be the same as the
          length of the string itself if all the characters are valid, otherwise
          we print an error message displaying the location of the offending
          character and continue.

          The strtol function will skip leading whitespace and attempt to convert
          the numeric part of the string to a long int. If an underflow or
          overflow occurs then strtol returns LONG_MIN or LONG_MAX respectively
          and sets errno to ERANGE. We just check errno and print an error if
          appropriate, you could compare l to LONG_MIN or LONG_MAX to determine
          the type of range error if desired.

          If everything goes well, the converted number is displayed.

          A couple of notes:

          If the input consists of multiple numbers seperated by whitespace, only
          the first one will be converted, you didn't specify what should happen
          in this case.

          If the input contains only of whitespace or the minus sign the
          converted value will be 0.

          Entering more than BUF_MAX characters at a time may cause unintended
          (but not undefined) behavior.

          If an invalid input line contains a tab character before the invalid
          character, the error message may not correctly point to the invalid
          character.

          Addressing these caveats is left as an exercise to the reader.

          Robert Gamble

          Comment

          • pemo

            #6
            Re: How to sscanf return integer only

            Barry Schwarz wrote:[color=blue]
            > On Thu, 4 May 2006 20:23:56 -0400, "Yogi_Bear_ 79" <nospam@spamsux .com>
            > wrote:
            >[color=green]
            >> I have the following code:
            >>
            >> sscanf(line, "%d", n_ptr) !=1 || n_ptr <=0;[/color]
            >
            > The third argument needs an & but I assume that is a typo.[/color]

            <snip>

            From its name, I would guess that n_ptr is set to point to an int and is
            defined as

            int * n_ptr;


            --
            ==============
            Not a pedant
            ==============


            Comment

            • Yogi_Bear_79

              #7
              Re: How to sscanf return integer only


              "Barry Schwarz" <schwarzb@doezl .net> wrote in message
              news:arcl52tii7 5m0q3uhsjbfel7g 1p0b8qnmd@4ax.c om...[color=blue]
              > On Thu, 4 May 2006 20:23:56 -0400, "Yogi_Bear_ 79" <nospam@spamsux .com>
              > wrote:
              >[color=green]
              >>I have the following code:
              >>
              >>sscanf(line , "%d", n_ptr) !=1 || n_ptr <=0;[/color]
              >
              > The third argument needs an & but I assume that is a typo.
              >
              > If you want to know whether the data at line is a "well formed"
              > integer, use strtol which will tell you where it stopped extracting
              > data. Then examine that char for white space or '\0'.
              >[color=green]
              >>
              >>It only partially works. If the user types a character other than 0-9 to
              >>start the string it fails. However as long as the first character is an
              >>integer it will allow letters in the following places
              >>
              >>for Example:
              >>
              >>H78 (fails as expected)
              >>78 (works as expected)
              >>7H8 ( doesn't fail as desired)
              >>
              >>[/color][/color]

              I think I understand now. The code as written is reading the line up until
              it encounters a non-integer. So 7 & 7H8 both return the same results. And
              from yor sufggestion above it will stop extracting at the H which I can use
              strtol to examine, and if it stops on anything other than white spaces I
              have my error?[color=blue][color=green]
              >>
              >>
              >>[/color]
              >
              >
              > Remove del for email[/color]


              Comment

              • Barry Schwarz

                #8
                Re: How to sscanf return integer only

                On Fri, 5 May 2006 09:15:57 +0100, "pemo" <usenetmeister@ gmail.com>
                wrote:
                [color=blue]
                >Barry Schwarz wrote:[color=green]
                >> On Thu, 4 May 2006 20:23:56 -0400, "Yogi_Bear_ 79" <nospam@spamsux .com>
                >> wrote:
                >>[color=darkred]
                >>> I have the following code:
                >>>
                >>> sscanf(line, "%d", n_ptr) !=1 || n_ptr <=0;[/color]
                >>
                >> The third argument needs an & but I assume that is a typo.[/color]
                >
                ><snip>
                >
                >From its name, I would guess that n_ptr is set to point to an int and is
                >defined as
                >
                >int * n_ptr;[/color]

                How often do you compare a pointer to less than or equal to zero? If
                he had coded *n_ptr <= 0 I would agree with you. Let's agree the code
                as written is confusing.


                Remove del for email

                Comment

                • Peter Shaggy Haywood

                  #9
                  Re: How to sscanf return integer only

                  Groovy hepcat Yogi_Bear_79 was jivin' on Thu, 4 May 2006 20:23:56
                  -0400 in comp.lang.c.
                  How to sscanf return integer only's a cool scene! Dig it!
                  [color=blue]
                  >I have the following code:
                  >
                  >sscanf(line, "%d", n_ptr) !=1 || n_ptr <=0;[/color]

                  Presumably n_ptr is a pointer. If not, then not only is it
                  misleadingly named, but it is also causing undefined behaviour since
                  sscanf() wants a pointer there. And if n_ptr is a pointer, then the
                  comparison n_ptr <= 0 makes little or no sense. Perhaps you meant
                  *n_ptr <= 0?
                  Also, this line reads in a number (assuming all is correct) and
                  checks for an error, but takes no action if an error has been
                  determined to have occurred. Rather pointless, isn't it?
                  [color=blue]
                  >It only partially works. If the user types a character other than 0-9 to
                  >start the string it fails. However as long as the first character is an
                  >integer it will allow letters in the following places
                  >
                  >for Example:
                  >
                  >H78 (fails as expected)
                  >78 (works as expected)
                  >7H8 ( doesn't fail as desired)[/color]

                  If you must use sscanf() for this, try it this way:

                  char dummy;
                  ....
                  if(sscanf(line, "%d%c", n_ptr, dummy) != 1)
                  {
                  /* Non-numerical input detected. Handle this error somehow. */
                  }

                  --

                  Dig the even newer still, yet more improved, sig!


                  "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
                  I know it's not "technicall y correct" English; but since when was rock & roll "technicall y correct"?

                  Comment

                  • Joe Smith

                    #10
                    Re: How to sscanf return integer only


                    "Robert Gamble" <rgamble99@gmai l.com[color=blue][color=green]
                    >> Yogi_Bear_79 wrote:[color=darkred]
                    >> > But there _is_ an integer - 7. there's then stuff after the integer.[/color]
                    >>
                    >> It needs to fail if there are any characters other than integers or white
                    >> space
                    >>
                    >>[color=darkred]
                    >> > and what's "|| n_ptr <=0" supposed to do?[/color]
                    >>
                    >> Not needed, removed, same results[/color]
                    >
                    > sscanf isn't the best tool for the job for a couple of reasons, not the
                    > least of which is that trying to convert a number of too large a
                    > magnitude results in undefined behavior.
                    >
                    > The following program will read a line at a time from stdin and attempt
                    > to convert it to an integer using the strtol function. If the line
                    > contains any invalid characters, an error message is displayed and the
                    > offending character is pointed out. If a range error occurs (the
                    > number was too large/small to store in a long int), an appropriate
                    > message is diaplayed. This version uses 'long int' and accepts
                    > negative numbers, remove the minus sign from the string of allowed
                    > characters if you just want to accept positive numbers. The code is a
                    > little verbose since it implements error checking.
                    >
                    > #include <stdio.h>
                    > #include <string.h>
                    > #include <stdlib.h>
                    > #include <errno.h>
                    > #define BUF_MAX 80
                    >
                    > int main (void) {
                    > char buf[BUF_MAX];
                    > long l;
                    > size_t s;
                    > while(fgets(buf , BUF_MAX, stdin) != NULL) {
                    > if ((s = strspn(buf, "-0123456789 \t\n")) == strlen(buf)) {
                    > errno = 0;
                    > l = strtol(buf, NULL, 10);
                    > if (errno == ERANGE) {
                    > printf("number out of range!\n");
                    > } else {
                    > printf("number read was %ld\n", l);
                    > }
                    > } else {
                    > while(s--)
                    > putchar(' ');
                    > putchar('^');
                    > printf(" invalid character\n");
                    > }
                    > }
                    > return 0;
                    > }
                    >
                    > The strspn function returns the number of characters in the input
                    > string that are in the "allow" string, this must be the same as the
                    > length of the string itself if all the characters are valid, otherwise
                    > we print an error message displaying the location of the offending
                    > character and continue.
                    >
                    > The strtol function will skip leading whitespace and attempt to convert
                    > the numeric part of the string to a long int. If an underflow or
                    > overflow occurs then strtol returns LONG_MIN or LONG_MAX respectively
                    > and sets errno to ERANGE. We just check errno and print an error if
                    > appropriate, you could compare l to LONG_MIN or LONG_MAX to determine
                    > the type of range error if desired.
                    >
                    > If everything goes well, the converted number is displayed.
                    >
                    > A couple of notes:
                    >
                    > If the input consists of multiple numbers seperated by whitespace, only
                    > the first one will be converted, you didn't specify what should happen
                    > in this case.
                    >
                    > If the input contains only of whitespace or the minus sign the
                    > converted value will be 0.
                    >
                    > Entering more than BUF_MAX characters at a time may cause unintended
                    > (but not undefined) behavior.
                    >
                    > If an invalid input line contains a tab character before the invalid
                    > character, the error message may not correctly point to the invalid
                    > character.
                    >
                    > Addressing these caveats is left as an exercise to the reader.
                    >
                    > Robert Gamble[/color]

                    I am unable to discern the OP's intent. [snipped from OP:]
                    sscanf(line, "%d", n_ptr) !=1 || n_ptr <=0;
                    would have wanted to come back with a bunch of concatenated digits. What
                    if, instead, you wanted a robust means of coming back with an unsigned long?
                    Elsethread, you hinted that strtol might not be a bad idea.
                    long strtol(const char *s, char **endp, int base)
                    The arguments look quite cryptic to me. Joe


                    Comment

                    • Robert Gamble

                      #11
                      Re: How to sscanf return integer only

                      Joe Smith wrote:[color=blue]
                      > "Robert Gamble" <rgamble99@gmai l.com[color=green][color=darkred]
                      > >> Yogi_Bear_79 wrote:
                      > >> > But there _is_ an integer - 7. there's then stuff after the integer.
                      > >>
                      > >> It needs to fail if there are any characters other than integers or white
                      > >> space
                      > >>
                      > >>
                      > >> > and what's "|| n_ptr <=0" supposed to do?
                      > >>
                      > >> Not needed, removed, same results[/color]
                      > >
                      > > sscanf isn't the best tool for the job for a couple of reasons, not the
                      > > least of which is that trying to convert a number of too large a
                      > > magnitude results in undefined behavior.
                      > >
                      > > The following program will read a line at a time from stdin and attempt
                      > > to convert it to an integer using the strtol function. If the line
                      > > contains any invalid characters, an error message is displayed and the
                      > > offending character is pointed out. If a range error occurs (the
                      > > number was too large/small to store in a long int), an appropriate
                      > > message is diaplayed. This version uses 'long int' and accepts
                      > > negative numbers, remove the minus sign from the string of allowed
                      > > characters if you just want to accept positive numbers. The code is a
                      > > little verbose since it implements error checking.
                      > >
                      > > #include <stdio.h>
                      > > #include <string.h>
                      > > #include <stdlib.h>
                      > > #include <errno.h>
                      > > #define BUF_MAX 80
                      > >
                      > > int main (void) {
                      > > char buf[BUF_MAX];
                      > > long l;
                      > > size_t s;
                      > > while(fgets(buf , BUF_MAX, stdin) != NULL) {
                      > > if ((s = strspn(buf, "-0123456789 \t\n")) == strlen(buf)) {
                      > > errno = 0;
                      > > l = strtol(buf, NULL, 10);
                      > > if (errno == ERANGE) {
                      > > printf("number out of range!\n");
                      > > } else {
                      > > printf("number read was %ld\n", l);
                      > > }
                      > > } else {
                      > > while(s--)
                      > > putchar(' ');
                      > > putchar('^');
                      > > printf(" invalid character\n");
                      > > }
                      > > }
                      > > return 0;
                      > > }
                      > >
                      > > The strspn function returns the number of characters in the input
                      > > string that are in the "allow" string, this must be the same as the
                      > > length of the string itself if all the characters are valid, otherwise
                      > > we print an error message displaying the location of the offending
                      > > character and continue.
                      > >
                      > > The strtol function will skip leading whitespace and attempt to convert
                      > > the numeric part of the string to a long int. If an underflow or
                      > > overflow occurs then strtol returns LONG_MIN or LONG_MAX respectively
                      > > and sets errno to ERANGE. We just check errno and print an error if
                      > > appropriate, you could compare l to LONG_MIN or LONG_MAX to determine
                      > > the type of range error if desired.
                      > >
                      > > If everything goes well, the converted number is displayed.
                      > >
                      > > A couple of notes:
                      > >
                      > > If the input consists of multiple numbers seperated by whitespace, only
                      > > the first one will be converted, you didn't specify what should happen
                      > > in this case.
                      > >
                      > > If the input contains only of whitespace or the minus sign the
                      > > converted value will be 0.
                      > >
                      > > Entering more than BUF_MAX characters at a time may cause unintended
                      > > (but not undefined) behavior.
                      > >
                      > > If an invalid input line contains a tab character before the invalid
                      > > character, the error message may not correctly point to the invalid
                      > > character.
                      > >
                      > > Addressing these caveats is left as an exercise to the reader.
                      > >
                      > > Robert Gamble[/color]
                      >
                      > I am unable to discern the OP's intent. [snipped from OP:]
                      > sscanf(line, "%d", n_ptr) !=1 || n_ptr <=0;
                      > would have wanted to come back with a bunch of concatenated digits.[/color]

                      In a followup the OP indicated that the "|| n_ptr <=0" was extraneous,
                      I haven't bothered to attempt to discern what its intended purpose
                      might have been but it doesn't seem to be relevant.
                      [color=blue]
                      > What
                      > if, instead, you wanted a robust means of coming back with an unsigned long?[/color]

                      I am not sure what you mean here, can you clarify?
                      If you mean modifying my example for unsigned long, just change the
                      type of l from long to unsigned long, remove the "-" from the list of
                      allowable characters, and replace the strtol with strtoul.
                      [color=blue]
                      > Elsethread, you hinted that strtol might not be a bad idea.[/color]

                      What do you mean by "elsethread "? I take that to mean elsewhere in
                      this thread but the only message I have posted in this thread, besides
                      this one, is the one you are responding to.
                      [color=blue]
                      > long strtol(const char *s, char **endp, int base)
                      > The arguments look quite cryptic to me.[/color]

                      Well, the prototype isn't meant to provide a detailed understanding of
                      how the function works, especially to someone who has never before
                      encountered it. The usage of the function is actually very
                      straightforward , check out your online documentation (man page, etc.),
                      pick up a good C book, or download n1124 for details.

                      Robert Gamble

                      Comment

                      • Joe Smith

                        #12
                        Re: How to sscanf return integer only


                        "Robert Gamble" <rgamble99@gmai l.com> wrote in message
                        news:1147026407 .265145.244920@ i39g2000cwa.goo glegroups.com.. .[color=blue]
                        > Joe Smith wrote:[color=green]
                        >> "Robert Gamble" <rgamble99@gmai l.com[/color][/color]
                        [color=blue][color=green][color=darkred]
                        >>>[Mr. Gamble's source and comments for a slightly different question
                        >>>snipped]
                        >> > If everything goes well, the converted number is displayed.[/color][/color][/color]
                        [color=blue][color=green]
                        >> What
                        >> if, instead, you wanted a robust means of coming back with an unsigned
                        >> long?[/color][/color]

                        [modified source:]
                        #include <stdio.h>
                        #include <string.h>
                        #include <stdlib.h>
                        #include <errno.h>
                        #define BUF_MAX 80

                        int main (void) {
                        char buf[BUF_MAX];
                        unsigned long l;
                        size_t s;
                        while(fgets(buf , BUF_MAX, stdin) != NULL) {
                        if ((s = strspn(buf, "0123456789 \t\n")) == strlen(buf)) {
                        errno = 0;
                        l = strtoul(buf, NULL, 10);
                        if (errno == ERANGE) {
                        printf("number out of range!\n");
                        } else {
                        printf("number read was %lu\n", l);
                        }
                        } else {
                        while(s--)
                        putchar(' ');
                        putchar('^');
                        printf(" invalid character\n");
                        }
                        }
                        return 0;
                        }
                        /* end source */
                        [color=blue]
                        > Well, the prototype isn't meant to provide a detailed understanding of
                        > how the function works, especially to someone who has never before
                        > encountered it. The usage of the function is actually very
                        > straightforward , check out your online documentation (man page, etc.),
                        > pick up a good C book, or download n1124 for details.[/color]


                        This compiles and seems to behave. The caveats he mentions for when this
                        situation would fail seem to have as a precondition that the person on the
                        keyboard had no business touching a computer. The man pages confused me.
                        Seemed to be all about Linux. I'm always trolling for a good C book. N
                        1124 sounds like a party. A little too far away for my vacation
                        preferences. Thanks for the source. Joe


                        Comment

                        • Robert Gamble

                          #13
                          Re: How to sscanf return integer only

                          Joe Smith wrote:[color=blue]
                          > I'm always trolling for a good C book. N
                          > 1124 sounds like a party. A little too far away for my vacation
                          > preferences. Thanks for the source. Joe[/color]

                          Check out "C Programming: A Modern Approach" by K. N. King and "C: A
                          Reference Manual (5th edition)" by Harbison & Steele. These are two of
                          the best, most comprehensive books out there.

                          Robert Gamble

                          Comment

                          • Joe Smith

                            #14
                            Re: How to sscanf return integer only


                            "Robert Gamble"[color=blue]
                            > Joe Smith wrote:[color=green]
                            >> I'm always trolling for a good C book. N
                            >> 1124 sounds like a party. A little too far away for my vacation
                            >> preferences. Thanks for the source. Joe[/color][/color]
                            [color=blue]
                            > Check out "C Programming: A Modern Approach" by K. N. King and "C: A
                            > Reference Manual (5th edition)" by Harbison & Steele. These are two of
                            > the best, most comprehensive books out there.[/color]

                            I just went to Amazon and got Harbison & Steele. I was stunned at how easy
                            it was, and I didn't have to pick through a bunch of garbage about C flat.
                            Makes a guy optimistic. Joe


                            Comment

                            • Dave Thompson

                              #15
                              Re: How to sscanf return integer only

                              On 4 May 2006 20:34:05 -0700, "Robert Gamble" <rgamble99@gmai l.com>
                              wrote:

                              <snip>[color=blue]
                              > sscanf isn't the best tool for the job for a couple of reasons, not the
                              > least of which is that trying to convert a number of too large a
                              > magnitude results in undefined behavior.
                              >
                              > The following program will read a line at a time from stdin and attempt
                              > to convert it to an integer using the strtol function. If the line
                              > contains any invalid characters, an error message is displayed and the
                              > offending character is pointed out. If a range error occurs (the
                              > number was too large/small to store in a long int), an appropriate
                              > message is diaplayed. This version uses 'long int' and accepts
                              > negative numbers, remove the minus sign from the string of allowed
                              > characters if you just want to accept positive numbers. The code is a
                              > little verbose since it implements error checking.
                              >
                              > #include <stdio.h>
                              > #include <string.h>
                              > #include <stdlib.h>
                              > #include <errno.h>
                              > #define BUF_MAX 80
                              >
                              > int main (void) {
                              > char buf[BUF_MAX];
                              > long l;
                              > size_t s;
                              > while(fgets(buf , BUF_MAX, stdin) != NULL) {
                              > if ((s = strspn(buf, "-0123456789 \t\n")) == strlen(buf)) {[/color]

                              Note that this accepts things like "212-535-1000".

                              A line read by fgets can never include \n, although if this were split
                              off to a separate function as it probably should be you might want to
                              retain that for other usages. Or perhaps you want(ed) \r.
                              [color=blue]
                              > errno = 0;
                              > l = strtol(buf, NULL, 10);
                              > if (errno == ERANGE) {
                              > printf("number out of range!\n");
                              > } else {
                              > printf("number read was %ld\n", l);
                              > }
                              > } else {
                              > while(s--)
                              > putchar(' ');
                              > putchar('^');[/color]

                              A cleverer(?) way to do this is: printf ("%*s^", (int)s, "")
                              or if you like ("%*c", (int)s+1, '^');
                              or other obvious variations.
                              [color=blue]
                              > printf(" invalid character\n");
                              > }
                              > }
                              > return 0;
                              > }
                              >
                              > The strspn function returns the number of characters in the input
                              > string that are in the "allow" string, this must be the same as the
                              > length of the string itself if all the characters are valid, otherwise
                              > we print an error message displaying the location of the offending
                              > character and continue.
                              >
                              > The strtol function will skip leading whitespace and attempt to convert
                              > the numeric part of the string to a long int. If an underflow or
                              > overflow occurs then strtol returns LONG_MIN or LONG_MAX respectively
                              > and sets errno to ERANGE. We just check errno and print an error if
                              > appropriate, you could compare l to LONG_MIN or LONG_MAX to determine
                              > the type of range error if desired.
                              >[/color]
                              strtol (and friends) will also return a pointer after the part of the
                              string it successfully converted; that's easier than getting your own
                              matching/validation logic to match exactly or even just right:
                              l = strtol (buf, &after, 10);
                              if( *after != '\0' ) /* wasn't (entirely) whitespace+numb er */
                              or
                              if( strspn (after, white) != strlen (after) )
                              if( after [strpsn (after, white)] != '\0' )
                              /* wasn't whitespace+numb er+whitespace */
                              etc.

                              <snip rest>

                              - David.Thompson1 at worldnet.att.ne t

                              Comment

                              Working...