On Tue, Sep 15, 2009 at 03:32:30PM -0700, Howard Sanner wrote:
> Is there a *portable* way in C to determine the size of a binary file?

Well, there's stat() if you've got it.  Returns a structure one, of
whose members is the file size.  Should work across any 'nix, dunno
about elsewhere.

> It would seem that
>
> fseek(stream, 0L, SEEK_END);
> fsize = ftell(stream);
>
> would do the trick. However, the standard does not require SEEK_END to  
> be implemented for binary files. (I looked it up last night.)

Actually, I think just the call to fseek() would suffice, since it
returns the current offset.  But yes, that still relies on SEEK_END
working the way it's supposed to, so if it's not implemented, then
no help there.

> I suspect this could be done quite easily in PERL or suchlike. The  
> problems are 1) I don't know PERL and don't have time or inclination to 
> learn it, and 2) the program has to run on a Windoze machine (= no  
> PERL). 

There's a Perl implementation for Windows, I believe; and one of the
nice things about Perl is that you can read an entire file into a
buffer without worrying about its size -- the space will be dynamically,
automagically allocated.  Now whether it's worth your time to climb
the learning curve for this -- I dunno.  But regular expression
support is excellent and it may well be faster to go this route rather
than to use C.

---Rsk

Reply via email to