home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / c / 16305 < prev    next >
Encoding:
Internet Message Format  |  1992-11-10  |  1.5 KB

  1. Path: sparky!uunet!munnari.oz.au!goanna!ok
  2. From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Reading unsigned char ?
  5. Summary: simple way
  6. Message-ID: <15882@goanna.cs.rmit.oz.au>
  7. Date: 11 Nov 92 03:11:57 GMT
  8. References: <jamull01.721293480@starbase.spd.louisville.edu>
  9. Organization: Comp Sci, RMIT, Melbourne, Australia
  10. Lines: 24
  11.  
  12. In article <jamull01.721293480@starbase.spd.louisville.edu>, jamull01@starbase.spd.louisville.edu (Mr. Positive) writes:
  13. > I am trying to use sscanf to read an unsigned character from an ASCII
  14. > file.  The only catch is what's in the file is not the ASCII character
  15. > but a decimal integer.  ... If anybody has any advice to offer I would
  16. > appreciate it.
  17.  
  18. sscanf() reads from a buffer in memory.  There is an easy method which will
  19. work with any member of the .scanf() family.  I'll illustrate with fscanf().
  20.  
  21.     #include <limits.h>
  22.     /* We want UCHAR_MAX */
  23.  
  24.     unsigned char this_is_were_we_really_want_it;
  25.     unsigned int we_shall_put_it_here_first;
  26.  
  27.     if (1 != fscanf(the_input_stream, "%u", &we_shall_put_it_here_first))
  28.         report_the_error();
  29.     assert(u < UCHAR_MAX);
  30.     this_is_where_we_really_want_it = we_shall_put_it_here_first;
  31.  
  32. In Classic C you could read numbers into [unsigned] [long] int, not into
  33. short or char.  This device of reading into a larger variable and then
  34. checking that the result is in range (omit the assert() if you have good
  35. reason to trust the data in your file) can be used quite generally.
  36.