delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/07/21/12:46:22

Date: Mon, 21 Jul 1997 12:42:18 -0400 (EDT)
From: "Art S. Kagel" <kagel AT ns1 DOT bloomberg DOT com>
To: "A. Sinan Unur" <asu1 AT cornell DOT edu>
Cc: djgpp AT delorie DOT com
Subject: Re: fread/fwite return value (CORRECTION)
In-Reply-To: <33CD4404.714D@cornell.edu>
Message-Id: <Pine.D-G.3.91.970721123332.17027K-100000@dg1>
Mime-Version: 1.0

On Wed, 16 Jul 1997, A. Sinan Unur wrote:

> A. Sinan Unur wrote:
> 
> off calling the function as
> 
> fread(buf, 1, size*n, stream)
> 
> instead of
> 
> fread(buf, size, n, stream)."
> 
> it follows that you can only count on fread returning zero in case of
> failure if you use the latter form.

No.  You can only depend on fread returning zero on error if you call it as:

fread(buf,size,n=1,stream);

If n > 1 fread may detect an error during processing of the n'th record 
or the n-1'st record in which case fread returns the number of records 
correctly and completely read.  I agree with P.J. Plauger about partial 
records with the proviso that iff you can extract useful information from 
a partial record or somehow recover the unread portion reading a single 
byte at a time may give you more control.  (Ex: stream is a socket and 
records are long enough to be broken into multiple IP packets and the 
connection is slow enough to timeout such that sleeping and rereading the 
stream may get the missing part of the record.)  Otherwise, who cares 
about partial records in the general case?  Usually if I do not get an 
entire record I need to just declare an error and recover.

Art S. Kagel, kagel AT bloomberg DOT com

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019