From: "A. Sinan Unur" Newsgroups: comp.os.msdos.djgpp Subject: Re: fread/fwite return value (CORRECTION) Date: Wed, 16 Jul 1997 17:58:28 -0400 Organization: Cornell University http://www.cornell.edu Lines: 47 Sender: asu1 AT cornell DOT edu (Verified) Message-ID: <33CD4404.714D@cornell.edu> References: <199707161515 DOT AA104406147 AT typhoon DOT rose DOT hp DOT com> <33CD1946 DOT 2795 AT cornell DOT edu> Reply-To: asu1 AT cornell DOT edu NNTP-Posting-Host: cu-dialup-0046.cit.cornell.edu Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk A. Sinan Unur wrote: > that is at best worthless advice. the documentation in this case is > plain wrong. it is a small mistake, but fread is supposed to return 0 > in case of error. oh, boy, do i love correcting myself. i checked the excerpt from the ansi standard in Plauger's "The Standard C Library". it says: 7.9.8.1 The fread function Synopsis #include size_t fread(void *ptr, size_t size, size_t nmemb, FILE* stream); ... Returns The fread function returns the number of elements successfully read, which may be less than nmemb if a read error or EOF is encountered. If size or nmemb is zero, fread returns zero and the contents of the array and the state of the stream remain unchanged. Later on, on p.271, he comments: "if the second argument is greater than one, you cannote determine whether the function also read up to size - 1 additional characters beyond what it reports. As a rule you are better 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. -- Sinan ******************************************************************* A. Sinan Unur WWWWWW |--O+O mailto:sinan DOT unur AT cornell DOT edu C ^ http://www.people.cornell.edu/pages/asu1/ \ ~/ Unsolicited e-mail is _not_ welcome, and will be billed for. *******************************************************************