delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/06/14/06:04:36

From: cziwkga AT ulcc DOT ac DOT uk (Kevin Ashley)
Newsgroups: comp.os.msdos.djgpp,comp.lang.c++
Subject: Re: Reading binary files
Date: 13 Jun 1997 13:45:04 GMT
Organization: University of London Computer Centre
Distribution: world
Message-ID: <5nrit0$mh1@calypso.ulcc.ac.uk>
References: <33a13a3a DOT 2140316 AT supernews DOT scsn DOT net>
Reply-To: k DOT ashley AT ulcc DOT ac DOT uk
NNTP-Posting-Host: silver.ulcc.ac.uk
Lines: 33
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

In article <33a13a3a DOT 2140316 AT supernews DOT scsn DOT net>, ckeenan AT scsn DOT net (Chris) writes:
|>
|>When reading in binary files, I need to know how to stop when reaching
|>the EOF.
|>
|>I know that stdio has a function fread but it doesn't check for end of
|>file.
|
Why do you say this ? fread does check for EOF. The return value of
the function is the number of items read. If this is 0, either EOF
has occurred or some other error.  The only complication
in using it is that, after it has returned 0, you must explicitly check
for EOF using feof() or ferror() if you need to distinguish this from some
other error condition. This is important if you are reading from
non-blocking sockets (when fread() can return 0 without an error having
occurred) but if you are doing this you probably know about
that. 
pseudo-code:

while(fread(buf,sizeof(short int),nints,f) {
  mumble buffer
}
if(!feof(f))
  fprintf(stderr,"reading failed\n");
fclose(f)


------------------------------------------------------------------------------
Kevin Ashley                              K DOT Ashley AT Ulcc DOT ac DOT uk
Development  Manager      http://www.ulcc.ac.uk/staff/Kevin+Ashley
University of London Computer Centre.      ...ukc!ncdlab!K.Ashley
                      This is not a signature

- Raw text -


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