www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/07/17/04:49:07

From: "A. Sinan Unur" <asu1 AT cornell DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: fread/fwite return value
Date: Wed, 16 Jul 1997 13:13:34 -0400
Organization: Cornell University
Lines: 37
Sender: asu1 AT cornell DOT edu (Verified)
Message-ID: <33CD013E.152B@cornell.edu>
References: <199707161302 DOT PAA20053 AT free DOT polbox DOT pl>
NNTP-Posting-Host: 128
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

heretic AT polbox DOT com wrote:
> So, I wanted to have error checking and I wrote something like
> 
>  if ( fread(blah,blah,blah) == -1 )
>  {
>     oh_no_error ();
>  }
> 

i think it is an error in the docs. AFAIK, fread is supposed to return
0, not EOF, precisely because the return value is size_t, and 0
signifies no bytes read. see the following routine:

C:\djgpp\c>fr fr.c

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  FILE* f;
  char c;

  f = fopen(argv[1], "rt");
  if (NULL == f)
  {
    perror(argv[1]);
    exit(EXIT_FAILURE);
  }

  while( fread(&c, 1, 1, f) )
    printf("%c", c);

  fclose(f);

  return EXIT_SUCCESS;
}

- Raw text -


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