Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: fread a file of unknown size |
| Date | 2015-03-08 18:43 -0700 |
| Organization | None to speak of |
| Message-ID | <871tkz3pys.fsf@kst-u.example.com> (permalink) |
| References | <mdi5g9$9hc$1@reader1.panix.com> <mdi64b$jpi$1@dont-email.me> <mdii07$6i0$1@dont-email.me> |
James Kuyper <jameskuyper@verizon.net> writes:
> On 03/08/2015 02:59 PM, Richard Heathfield wrote:
>> On 08/03/15 18:47, ruben safir wrote:
>>> if you use fread to read a file of unknown size with a set buffer size
>>> of say 500 bytes, when you get to the end of the file you are likely to
>>> read a stub of 500 bytes of info and the eof marker.
>>>
>>> And fread will not red that block
>>
>> fread ought to return the number of bytes read. Instead, it returns the
>> number of blocks. I expect there was a good reason once.
>
> I gather that there are systems, or at least particular devices on some
> systems, where the number of bytes read will always be a multiple of a
> block size characteristic of that device.
But fread() is defined as a sequence of calls to fgetc() (though of
course it doesn't literally have to call fgetc()). N1570 7.21.8.1p2:
The fread function reads, into the array pointed to by ptr,
up to nmemb elements whose size is specified by size, from
the stream pointed to by stream. For each object, size calls
are made to the fgetc function and the results stored, in the
order read, in an array of unsigned char exactly overlaying the
object. The file position indicator for the stream (if defined)
is advanced by the number of characters successfully read. If an
error occurs, the resulting value of the file position indicator
for the stream is indeterminate. If a partial element is read,
its value is indeterminate.
> Even on systems where that is not true, if the file I'm reading has a
> fixed record size, a valid read from an undamaged file will always read
> in an integer number of records, so it still makes sense.
fread() attempts to read a specified number of elements, where you can
specify the size in bytes of each element.
The question is, what do you want to do if reading an element (record)
is only partly successful. Say you're reading 1024-byte records from a
file that's 10752 bytes long (that's exactly 10.5 records). If you care
how many bytes of that last partial record were read, use a size of 1.
If you don't care (i.e., if a partial read is simply a failure), use a
size equal to the record size.
I discussed this issue here: http://stackoverflow.com/a/8589688/827263
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
fread a file of unknown size ruben safir <ruben@mrbrklyn.com> - 2015-03-08 14:47 -0400
Re: fread a file of unknown size Richard Heathfield <rjh@cpax.org.uk> - 2015-03-08 18:59 +0000
Re: fread a file of unknown size James Kuyper <jameskuyper@verizon.net> - 2015-03-08 18:21 -0400
Re: fread a file of unknown size Keith Thompson <kst-u@mib.org> - 2015-03-08 18:43 -0700
Re: fread a file of unknown size fmassei@gmail.com - 2015-03-08 15:41 -0700
Re: fread a file of unknown size raltbos@xs4all.nl (Richard Bos) - 2015-03-10 16:04 +0000
Re: fread a file of unknown size Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-03-08 18:44 -0400
Re: fread a file of unknown size jacob navia <jacob@spamsink.net> - 2015-03-08 20:32 +0100
Re: fread a file of unknown size Keith Thompson <kst-u@mib.org> - 2015-03-08 12:48 -0700
Re: fread a file of unknown size glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-03-08 23:39 +0000
Re: fread a file of unknown size James Kuyper <jameskuyper@verizon.net> - 2015-03-08 20:40 -0400
Re: fread a file of unknown size glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-03-09 01:24 +0000
Re: fread a file of unknown size Keith Thompson <kst-u@mib.org> - 2015-03-09 07:53 -0700
Re: fread a file of unknown size glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-03-09 17:36 +0000
Re: fread a file of unknown size Keith Thompson <kst-u@mib.org> - 2015-03-09 10:44 -0700
Re: fread a file of unknown size glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-03-09 20:07 +0000
csiph-web