Mail Archives: djgpp/1997/06/14/06:04:36
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 -