Board index » delphi » Function fileSize() with untyped File

Function fileSize() with untyped File

*****************************************************3
I have a small problem with Pascal with Objects.
The problem is the function: fileSize()!
When I open an untyped File this function doesn't give back
the right size of the passed file.

You can try with this piece of Program:

program FileSize_1;

type

     UFile = file;

var
    myFile : UFile;

begin
   assign(myFile, paramstr(1));
   reset(myFile);
   writeln('The file size is: ', fileSize(myFile));
   close(myFile);
end.

This is the same program contained in the manual where is explained
the function fileSize().

If you run this program the function fileSize() give back a wrong size.
Try now to change the type in:

    UFile = file of Byte;

and rerun again the program. In the second case the value of FileSize is correct.

I thougth that if the compiler doesn't know the type of the file it cannot calculate
the size. But this explenation sound very strange.
Could you give me an answer about this problem?

Now another question: 'What happen if on the function BlockRead(File, buf, byte)
the variable buf is a Pointer and not a Static var? Does the compiler work correctly?

Thank you for your attention and excuse me my easy english with mistakes!

Ivano Bossi (E-Mail: chska...@ibmmail.com)

 

Re:Function fileSize() with untyped File


In article 4...@media.ska.com, wim...@dial.eunet.ch writes:

Quote
>*****************************************************3
>I have a small problem with Pascal with Objects.
>The problem is the function: fileSize()!
>When I open an untyped File this function doesn't give back
>the right size of the passed file.

>You can try with this piece of Program:

>program FileSize_1;
>type
>     UFile = file;
>var
>    myFile : UFile;
>begin
>   assign(myFile, paramstr(1));
>   reset(myFile);
>   writeln('The file size is: ', fileSize(myFile));
>   close(myFile);
>end.

Reset on untyped files will by defualt be Reset(myFile, 128);
You need to use Reset(myFile, 1); Filesize will be returning the number of
128 byte records in your file !

Quote
>Now another question: 'What happen if on the function BlockRead(File, buf, byte)
>the variable buf is a Pointer and not a Static var? Does the compiler work correctly?

You must of course make sure that buf is defined and created
Var
  buf : ^Array[1..100] of Byte;
  F : File;
Begin
  GetMem(Buf, 100);
  Assign(F, 'MyFile.Dat');
  Reset(F,1);
  BlockRead(F, Buf^, 100);
...
...

HTH

Ray Cramer
R...@Pol.ac.uk

Re:Function fileSize() with untyped File


Quote
wim...@dial.eunet.ch wrote:

: *****************************************************3
: I have a small problem with Pascal with Objects.
: The problem is the function: fileSize()!
: program FileSize_1;
: type
:      UFile = file;
: var
:     myFile : UFile;
: begin
:    assign(myFile, paramstr(1));
:    reset(myFile);
     reset(myFile,1)
:    writeln('The file size is: ', fileSize(myFile));
:    close(myFile);
: end.

With the new line everything should be fine.

--
=============================================================================
Dipl. Phys. Hans L. Trautenberg                       Universitaet Regensburg
                           Institut fuer Experimentelle und Angewandte Physik
phone (49) 941 943 2466                                         Polymerphysik
fax   (49) 941 943 3196                                    D-93040 Regensburg
e-mail  hans.trautenb...@physik.uni-regensburg.de                     Germany

privat
phone (49) 941 949211                                             Flurstr. 14
fax   (49) 941 930792                                            D-92348 Berg
                                                                      Germany
=============================================================================

Re:Function fileSize() with untyped File


use
 reset( f, 1 );
I think that this is actually a DOS limitation not a BP one.  Personally I
never type files, just reset( f, 1 ) and blockread(), it goes several thousand
(maybe a slight exaggeration there) times faster, especially if you want to
read in arrays etc.

part 2 :
NO!!!!  Do that and 10 bucks says you'll overwrite something critical.  That
would read in a POINTER itself, NOT what it references.  Use
    blockread( f, buf^ { <- deference }, size );
and note that you DO need to allocate the buffer beforehand (i.e. GetMem or
New).

Have fun,
Will Bryant
brya...@winston.riccarton.school.nz

Quote
>*****************************************************3
>I have a small problem with Pascal with Objects.
>The problem is the function: fileSize()!
>When I open an untyped File this function doesn't give back
>the right size of the passed file.
>You can try with this piece of Program:
>program FileSize_1;
>type
>     UFile = file;
>var
>    myFile : UFile;
>begin
>   assign(myFile, paramstr(1));
>   reset(myFile);
>   writeln('The file size is: ', fileSize(myFile));
>   close(myFile);
>end.
>This is the same program contained in the manual where is explained
>the function fileSize().
>If you run this program the function fileSize() give back a wrong size.
>Try now to change the type in:
>    UFile = file of Byte;
>and rerun again the program. In the second case the value of FileSize is correct.
>I thougth that if the compiler doesn't know the type of the file it cannot calculate
>the size. But this explenation sound very strange.
>Could you give me an answer about this problem?
>Now another question: 'What happen if on the function BlockRead(File, buf, byte)
>the variable buf is a Pointer and not a Static var? Does the compiler work correctly?
>Thank you for your attention and excuse me my easy english with mistakes!
>Ivano Bossi (E-Mail: chska...@ibmmail.com)

Re:Function fileSize() with untyped File


Quote
>I have a small problem with Pascal with Objects.
>The problem is the function: fileSize()!
>When I open an untyped File this function doesn't give back
>the right size of the passed file.

><program listing omitted>

Is the size off by a factor of 128? The default record size, if
the second parameter to reset() is omitted, a size of 128 is
assumed. fileSize() returns the number of components in an untyped
file (NOT the number of bytes). The solution is simple: use
recsize(file,1) to make the components 1 byte long.

Re:Function fileSize() with untyped File


Funtion FileSize(f) returns the number of components in f, not the
number of bytes. So, if f is a file of LongInt, FileSize(f) gives
the number of LongInt in f, which is the number of bytes divided
by 4. For an untyped file, the default record size is 128 bytes.
In order to get the number of bytes, you must define the record
size as 1 byte; this is done with the command Reset(f,1).

ba...@prometheus.hol.gr

Other Threads