Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #72011

Re: Reading beyond the end of file

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Reading beyond the end of file
Date 2015-10-05 15:31 -0700
Organization None to speak of
Message-ID <lnlhbh3q1x.fsf@kst-u.example.com> (permalink)
References (8 earlier) <musu7o$v4j$1@speranza.aioe.org> <lnio6l6gch.fsf@kst-u.example.com> <muthba$83c$1@speranza.aioe.org> <lntwq53zw4.fsf@kst-u.example.com> <muuqmc$gjo$1@speranza.aioe.org>

Show all headers | View raw


glen herrmannsfeldt <gah@ugcs.caltech.edu> writes:
> Keith Thompson <kst-u@mib.org> wrote:
[...]
>>> And what about the sizeof(char)==sizeof(int) case?
>  
>> In that case, if getchar() returns EOF you can't tell whether it
>> indicates that there are no more characters to be read, or whether it
>> actually read a character with that value (0xFFFF if CHAR_BIT==16, for
>> example).  You can call feof() and ferror() after getchar() returns EOF
>> to distinguish those cases.
>
> I was thinking about UTF-32, but it also occurs for UTF-16 on
> systems with 16 bit int.

Plenty of systems support UTF-16 without making CHAR_BIT > 8.

>> Another problem is that the input character is treated as an unsigned
>> char and converted to signed int.  If unsigned char fits in the range of
>> signed int, that's not a problem.  But if not, and if you read a
>> character with a value exceeding INT_MAX, then the conversion to int
>> yields an implementation-defined result or raises an
>> implementation-defined signal.  Probably the conversion will do the
>> typical 2's-complement wraparound, but if it doesn't, hilarity ensues.
>
> Seems to me that this already happens on systems where char is signed.
>
> The (char) is converted to (unsigned char) for fgetc(), then after
> reading it back to (char). 
> 
> Is (char) conversion defined different from (int) conversion?

There's no such conversion.  N1570 7.21.7.1p1:

    If the end-of-file indicator for the input stream pointed to by
    stream is not set and a next character is present, the fgetc
    function obtains that character as an unsigned char converted
    to an int and advances the associated file position indicator
    for the stream (if defined).

It doesn't read the character as a (plain) char and then convert it to
unsigned char.  It reads it directly as an unsigned char.  Before the
read, it doesn't have a type; it's just bits in whatever the input
stream is reading from.

>> In practice, I don't think there are any hosted implementations with
>> UCHAR_MAX > INT_MAX, and freestanding implementations aren't required to
>> provide getchar() and friends.
>
> I suspect UTF-16 will get more popular, but UTF-32 will likely 
> stay rare for a while.

Neither of those requires CHAR_BIT > 8.  See char16_t and char32_t in
<uchar.h> (new in C11).

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Reading beyond the end of file Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-11 10:06 -0700
  Re: Reading beyond the end of file Geoff <geoff@invalid.invalid> - 2015-09-11 12:41 -0700
    Re: Reading beyond the end of file Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-26 14:24 -0700
      Re: Reading beyond the end of file Geoff <geoff@invalid.invalid> - 2015-09-26 21:49 -0700
        Re: Reading beyond the end of file Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-01 13:57 -0700
          Re: Reading beyond the end of file Geoff <geoff@invalid.invalid> - 2015-10-01 17:22 -0700
            Re: Reading beyond the end of file raltbos@xs4all.nl (Richard Bos) - 2015-10-02 13:05 +0000
            Re: Reading beyond the end of file Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-04 17:56 -0700
  Re: Reading beyond the end of file Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2015-09-11 13:55 -0600
    Re: Reading beyond the end of file Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-26 14:14 -0700
      Re: Reading beyond the end of file Siri Cruz <chine.bleu@yahoo.com> - 2015-09-26 14:43 -0700
        Re: Reading beyond the end of file Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-09-26 17:57 -0400
        Re: Reading beyond the end of file Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-01 14:01 -0700
      Re: Reading beyond the end of file Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2015-09-26 22:18 -0600
        Re: Reading beyond the end of file Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-01 12:58 -0700
          Re: Reading beyond the end of file Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2015-10-01 15:08 -0600
            Re: Reading beyond the end of file Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-04 18:03 -0700
              Re: Reading beyond the end of file glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-05 04:02 +0000
      Re: Reading beyond the end of file raltbos@xs4all.nl (Richard Bos) - 2015-10-02 13:11 +0000
        Re: Reading beyond the end of file supercat@casperkitty.com - 2015-10-02 09:31 -0700
          Re: Reading beyond the end of file Richard Damon <Richard@Damon-Family.org> - 2015-10-03 16:07 -0400
        Re: Reading beyond the end of file Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-04 19:06 -0700
  Re: Reading beyond the end of file Keith Thompson <kst-u@mib.org> - 2015-09-11 14:36 -0700
    Re: Reading beyond the end of file Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-26 14:05 -0700
      Re: Reading beyond the end of file Keith Thompson <kst-u@mib.org> - 2015-09-26 18:53 -0700
        Re: Reading beyond the end of file Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-01 13:49 -0700
          Re: Reading beyond the end of file Richard Damon <Richard@Damon-Family.org> - 2015-10-01 20:23 -0400
            Re: Reading beyond the end of file glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-02 06:14 +0000
            Re: Reading beyond the end of file Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-04 19:03 -0700
              Re: Reading beyond the end of file Richard Damon <Richard@Damon-Family.org> - 2015-10-04 22:55 -0400
                Re: Reading beyond the end of file glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-05 04:27 +0000
                Re: Reading beyond the end of file Keith Thompson <kst-u@mib.org> - 2015-10-04 22:21 -0700
                Re: Reading beyond the end of file glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-05 09:53 +0000
                Re: Reading beyond the end of file James Kuyper <jameskuyper@verizon.net> - 2015-10-05 13:15 -0400
                Re: Reading beyond the end of file supercat@casperkitty.com - 2015-10-05 10:25 -0700
                Re: Reading beyond the end of file glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-05 21:30 +0000
                Re: Reading beyond the end of file glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-05 21:27 +0000
                Re: Reading beyond the end of file supercat@casperkitty.com - 2015-10-05 10:17 -0700
                Re: Reading beyond the end of file Keith Thompson <kst-u@mib.org> - 2015-10-05 11:59 -0700
                Re: Reading beyond the end of file supercat@casperkitty.com - 2015-10-05 13:44 -0700
                Re: Reading beyond the end of file glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-05 21:58 +0000
                Ones' complement subtraction supercat@casperkitty.com - 2015-10-05 15:28 -0700
                Re: Ones' complement subtraction glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-06 02:14 +0000
                Re: Ones' complement subtraction supercat@casperkitty.com - 2015-10-06 08:48 -0700
                Re: Ones' complement subtraction glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-06 20:55 +0000
                Re: Ones' complement subtraction supercat@casperkitty.com - 2015-10-06 14:41 -0700
                Re: Ones' complement subtraction glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-07 00:14 +0000
                Re: Reading beyond the end of file Keith Thompson <kst-u@mib.org> - 2015-10-05 15:24 -0700
                Re: Reading beyond the end of file glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-05 21:39 +0000
                Re: Reading beyond the end of file Keith Thompson <kst-u@mib.org> - 2015-10-05 15:31 -0700
                Re: Reading beyond the end of file glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-06 03:04 +0000
                Re: Reading beyond the end of file Keith Thompson <kst-u@mib.org> - 2015-10-06 08:55 -0700
                Re: Reading beyond the end of file supercat@casperkitty.com - 2015-10-06 11:44 -0700
                Re: Reading beyond the end of file Richard Damon <Richard@Damon-Family.org> - 2015-10-05 23:16 -0400
                Re: Reading beyond the end of file James Kuyper <jameskuyper@verizon.net> - 2015-10-06 10:29 -0400
                Re: Reading beyond the end of file Keith Thompson <kst-u@mib.org> - 2015-10-06 08:56 -0700
                Re: Reading beyond the end of file Richard Damon <Richard@Damon-Family.org> - 2015-10-06 23:13 -0400
                Re: Reading beyond the end of file supercat@casperkitty.com - 2015-10-06 22:01 -0700
                Re: Reading beyond the end of file Keith Thompson <kst-u@mib.org> - 2015-10-07 08:43 -0700
                Re: Reading beyond the end of file supercat@casperkitty.com - 2015-10-07 10:02 -0700
                Re: Reading beyond the end of file Keith Thompson <kst-u@mib.org> - 2015-10-07 13:08 -0700
                Re: Reading beyond the end of file Richard Damon <Richard@Damon-Family.org> - 2015-10-07 22:03 -0400
                Re: Reading beyond the end of file Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-05 01:47 -0700

csiph-web