home *** CD-ROM | disk | FTP | other *** search
- From: twv@hpcss01.cup.hp.com (Terry Von Gease)
- Date: Wed, 27 Jan 1993 19:29:59 GMT
- Subject: Re: strtol() return values
- Message-ID: <19460002@hpcss01.cup.hp.com>
- Organization: the Lodi Home for the Bald
- Path: sparky!uunet!ftpbox!news.acns.nwu.edu!zaphod.mps.ohio-state.edu!caen!sdd.hp.com!col.hp.com!news.dtc.hp.com!hpscit.sc.hp.com!hplextra!hpcss01!twv
- Newsgroups: comp.lang.c
- References: <ric.728002623@updike>
- Lines: 25
-
- >/ hpcss01:comp.lang.c / ric@updike..sri.com (Richard Steinberger) / 2:57 pm Jan 25, 1993 /
- >
- > strtol() returns a long int when successful. If a non-numeric
- >string is encountered, 0 is returned. Unfortunately, errno doesn't seem
- >to be set. Here's the question: If a string like, "0" or "000"
- >is encountered, I also get a 0 returned from strtol(). Is there an easy,
- >elegant way to check for this without examing each char?
- >
- >ric steinberger
- >ric@updike.sri.com
- >
-
- Try this....
-
- char *cp;
-
- long_number=strtol(ascii_number,&cp,10);
- if (*cp) ERROR!!!!
-
- The pointer cp is set to the byte in the input string ascii_number that
- terminated the conversion. If the entire string was used then *cp will
- point to the 0 string terminator. If *cp is true then the string contained
- characters that strtol could not cope with.
-
- Terry
-