Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #58000

Re: fread a file of unknown size

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: fread a file of unknown size
Date 2015-03-08 12:48 -0700
Organization None to speak of
Message-ID <8761ab46e7.fsf@kst-u.example.com> (permalink)
References <mdi5g9$9hc$1@reader1.panix.com>

Show all headers | View raw


ruben safir <ruben@mrbrklyn.com> writes:
> 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 and set and oef that can be read with
> feof.  That is how I understand thinga.  So we can either use fseek to
> discover the file size and fread accordingly or use fstat?
>
> is this the proper approach?

You've already gotten some good answers.  One more thing: Don't use the
feof() function to decide when to stop reading.  Use the result returned
by whatever function you're using to read from the file (fread() in your
case).

feof() returns the value of the end-of-file flag for the file.  That
flag is set *after* you attempt to read past the end of the file -- and
it's not set if there was an error (that sets the error flag, which you
can query with ferror()).  So if there's an error reading from the file,
just calling feof() won't detect it, and you can have an infinite loop.

You can use feof() and/or ferror() *after* reaching the end of the file
to find out why there was no more input.

-- 
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


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