From owner-freebsd-hackers@FreeBSD.ORG Tue Mar 10 22:53:15 2009 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 7A2541065677 for ; Tue, 10 Mar 2009 22:53:15 +0000 (UTC) (envelope-from jimmy@mammothcheese.ca) Received: from smtp124.rog.mail.re2.yahoo.com (smtp124.rog.mail.re2.yahoo.com [206.190.53.29]) by mx1.freebsd.org (Postfix) with SMTP id 0ED0E8FC20 for ; Tue, 10 Mar 2009 22:53:14 +0000 (UTC) (envelope-from jimmy@mammothcheese.ca) Received: (qmail 62596 invoked from network); 10 Mar 2009 22:26:34 -0000 Received: from unknown (HELO ?72.138.160.206?) (jimmy@72.138.160.206 with plain) by smtp124.rog.mail.re2.yahoo.com with SMTP; 10 Mar 2009 22:26:34 -0000 X-YMail-OSG: gKhN.QoVM1l4_4YKpE5zfRlXIx.ck70K0Zsz2zZDaLP.efOdCojm5AgVK2lvcv4QDQ-- X-Yahoo-Newman-Property: ymail-3 Message-ID: <49B6E91A.4040604@mammothcheese.ca> Date: Tue, 10 Mar 2009 18:26:34 -0400 From: James Bailie User-Agent: Thunderbird 2.0.0.19 (X11/20090116) MIME-Version: 1.0 To: freebsd-hackers@freebsd.org References: <49B6DC95.9070607@FreeBSD.org> In-Reply-To: <49B6DC95.9070607@FreeBSD.org> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: fgetc doubts X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: jimmy@mammothcheese.ca List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 10 Mar 2009 22:53:15 -0000 fgetc() returns an int so that EOF may be distinguished from valid return values. Valid values are 8-bit values. EOF is a 32-bit value. EOF is a 32-bit two's-complement -1 (0xffffffff), and -1 input is 8-bit two's-complement -1 (0xff). When fgetc() casts this to an int, it becomes 0x000000ff, or 255, and thus the two values may be distinguished from each other. I haven't looked at your code, but you are probably comparing EOF with the value returned by fgetc() after it has been cast to a char. EOF is getting cast to a char implicitly in the comparison, so the comparison becomes a comparison between 0xff and 0xff. You need to test the int returned by fgetc() for EOF before assigning it to a char. Gábor Kövesdán wrote: > Hello, > > I have a problem when reading files with fgetc when a 0xff character > comes. In my code the reading stops at that point as if EOF had been > reached, but that's not actually the case. > The code is here: > http://p4web.freebsd.org/@md=d&cd=//&c=Nsd@//depot/projects/soc2008/gabor_textproc/grep/file.c?ac=64&rev1=40 > > And the problem occurs in grep_fgetln() when the buffers is being filled > in: > for (; i < bufsiz && !grep_feof(f); i++) > binbuf[i] = grep_fgetc(f); > > Thanks in advance, > -- James Bailie http://www.mammothcheese.ca