home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!haven.umd.edu!darwin.sura.net!wupost!gumby!destroyer!ncar!noao!amethyst!organpipe.uug.arizona.edu!news
- From: dave@cs.arizona.edu (Dave Schaumann)
- Newsgroups: comp.lang.c
- Subject: Re: compiler broken?
- Message-ID: <1992Jul27.183758.29832@organpipe.uug.arizona.edu>
- Date: 27 Jul 92 18:37:58 GMT
- References: <1992Jul25.054848.11223@wyvern.twuug.com>
- Sender: news@organpipe.uug.arizona.edu
- Reply-To: dave@cs.arizona.edu (Dave Schaumann)
- Organization: University of Arizona
- Lines: 62
- In-Reply-To: alpha@wyvern.twuug.com (Joe Wright)
-
- In article <1992Jul25.054848.11223@wyvern.twuug.com>, alpha@wyvern (Joe Wright) writes:
- >rdlin() {
- > int i;
- > for (i = 0; i < length && ((record[i] = getc(infile)) != EOF); ++i)
- > ;
- > return i;
- >}
- >
- >It worked fine on my C/80 system but failed on another i486 Unix
- >system. It turned out that if the character read was 0xff it was
- >promoted to -1 (EOF) and caused an 'early' exit.
-
- You left out the declaration of record[], but from your problem, I
- bet you have it declared as "char record[N]", or something similar.
- Moreover, I'll bet chars are unsigned on your C/80, and signed on
- the Unix box.
-
- Your problem (assuming my guesses are correct) is in the expression
-
- (record[i] = getc(infile)) != EOF
-
- If record[i] is of type char, then the value of the assignment statement
- will be of type char, too. That means if you have signed chars, 0xFF gets
- promoted to (int) -1, and matches EOF. If you have unsigned chars, this
- promotion does not happen.
-
- However, if you have unsigned chars, when EOF is encountered, the value
- is first truncated to 0xff by the assignment statement, so it never
- matches the EOF statement. That means all remaining records are filled
- with (char)EOF, which may or may not matter to the rest of your program.
-
- >I fixed it:
- >
- >rdlin() {
- > int c, i;
- > for (i = 0; i < length; ++i) {
- > if ((c = getc(infile)) == EOF)
- > break;
- > record[i] = c;
- > }
- > return i;
- >}
-
- Notice how here, the assignment statement results in an int, so no
- information is lost when EOF is encountered, and 0xff is not erroneously
- promoted when it is encountered.
-
- Also, you probably want to put a '\0' after the end of the data in your
- array, since many of C's string-handling functions depend on it being there.
- You should probably also re-think the wisdom of making "record", "length",
- and "infile" global to a routine of such general utility as reading in a
- string. In fact, you may wish to investigate fgets(), which is a library
- routine that performs virtually the same task as the code presented above.
-
- >Question: Is the Unix compiler broken?
-
- No, I don't think so.
-
- --
- You're traveling through another dimension -- a dimension not only of sight and
- sound but of mind. A journey into a wonderous land whose boundaries are that of
- imagination. That's a signpost up ahead: your next stop: the Twilight Zone!
-