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