[fpc-devel] Determin file size - how?
XHajT03 at hajny.biz
Thu Dec 15 10:29:05 CET 2011
On Thu, December 15, 2011 05:04, Hans-Peter Diettrich wrote:
> Tomas Hajny schrieb:
>>> I wonder how to obtain the size of an file on disk. The only function
>>> I could find so far is FileSize, which requires an open File, but
>>> nothing for an file name :-(
>>> ATM FileSize(TEXT) would help, too, but FileSize only accepts an FILE,
>>> not TEXT :-(
>> What exactly do you want to achieve?
> Effectively I'm missing a function FileSize(filename), similar to
> FileExists or FileAge, for general use.
Alright. Then something based on FindFirst is your solution.
>> Is it because the file may be
>> open as a text and denied sharing wouldn't allow parallel opening for
>> reading as an untyped file (as suggested by Vinzent)? If this is the
>> case, there is a nasty but completely platform independent hack which
>> you could use (based on the fact that the underlying operating
>> systems and their APIs make no difference between file and text).
> Normally I use TFileStream's, which have a Size property. But I find it
> nasty to open an file, when I only want to know its size. FPC (like
> Delphi) lacks some basic file/directory handling functions (Size, Remove,
> Rename...), at least none are listed in the "File handling overview".
> While a Delphi user can use the WinAPI for such tasks, a cross-platform
> development system should provide according platform-independent
Basic functionality for working with files is available in units System,
SysUtils and Dos - see e.g.
http://wiki.freepascal.org/Unit_categorization. Which 'File handling
overview' do you refer to (document and chapter)?
>> Or is it because you don't want to open the file at all (e.g. to avoid
>> modifying the timestamp of the last file access)? If this is the case,
>> you probably need to stick to FindFirst, etc.
> These subtle problems should be considered by the implementors of such
> basic general-purpose functions.
The point is that depending on your use case, these 'subtle problems' may
not be relevant at all and efficiency of possible alternative solutions
differs too (that 'subtle problem' doesn't exist if you already have a
file open anyway, even though it's open for text processing or TFileStream
or whatever, and the solution used in System.FileSize might be extended to
text files too with a fairly limited effort).
More information about the fpc-devel