Subj : Re: newbie question [C language] To : comp.programming From : Rob Thorpe Date : Fri Jul 01 2005 07:14 am CBFalconer wrote: > Rob Thorpe wrote: > > osmium wrote: > >> "Fred_NacH" writes: > >> > >>> I was wondering, how to retrieve the size/length of a file by > >>> only using the ansi functions... > >> > >> This one often causes a lot of controversy, some people say > >> there is a (usually unnamed) system on which it won't work or at > >> least there *could have been* a system on which it wouldn't work > >> if such a system had been built. > >> > >> Open the file in *binary* mode, seek to end of file, and use > >> ftell() to see where you are. If you like acrimony and pedantry, > >> look around on Usenet for these discussions which flare up from > >> time to time. > > > > I expect the problem Randy points out is something like a bug - > > or at least lies in the neverland between bug and feature. > > > > fseek to SEEK_END isn't gauranteed to work on a binary file. I > > think this is because on some operating systems binary files are > > made from fixed size block of some number of bytes. They appear > > like files in other systems, but with nulls at the end padding > > out to the end of the block. > > > > It's worth pointing out that stat/fstat or a version of it are > > available on many platforms even though they aren't standard. > > Try thinking about device files and sparse files. I forgot about that. On some platforms you may be able to get useful information from stat/fstat. On others you may get useful information from them on some volumes or devices, but not others. On some platforms you may be able to get it from fseek & ftell. On others they're may be system specific interfaces that do the job. Here be dragons drinking heartly from the blood of the leaking abstractions. .