home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / c / 20294 < prev    next >
Encoding:
Internet Message Format  |  1993-01-28  |  1.2 KB

  1. From: twv@hpcss01.cup.hp.com (Terry Von Gease)
  2. Date: Wed, 27 Jan 1993 19:29:59 GMT
  3. Subject: Re: strtol() return values
  4. Message-ID: <19460002@hpcss01.cup.hp.com>
  5. Organization: the Lodi Home for the Bald
  6. 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
  7. Newsgroups: comp.lang.c
  8. References: <ric.728002623@updike>
  9. Lines: 25
  10.  
  11. >/ hpcss01:comp.lang.c / ric@updike..sri.com (Richard Steinberger) /  2:57 pm  Jan 25, 1993 /
  12. >
  13. >    strtol() returns a long int when successful.  If a non-numeric
  14. >string is encountered, 0 is returned.  Unfortunately, errno doesn't seem
  15. >to be set.  Here's the question:  If a string like, "0" or "000"
  16. >is encountered, I also get a 0 returned from strtol(). Is there an easy,
  17. >elegant way to check for this without examing each char?  
  18. >
  19. >ric steinberger
  20. >ric@updike.sri.com
  21. >
  22.  
  23. Try this....
  24.  
  25.     char *cp;
  26.     
  27.     long_number=strtol(ascii_number,&cp,10);
  28.     if (*cp)  ERROR!!!!
  29.  
  30. The pointer cp is set to the byte in the input string ascii_number that
  31. terminated the conversion.  If the entire string was used then *cp will
  32. point to the 0 string terminator.  If *cp is true then the string contained
  33. characters that strtol could not cope with.
  34.  
  35. Terry
  36.