Quote Originally Posted by Colin
Quote Originally Posted by User137
[pascal]
// expected result is 100 !! but its is
[/pascal]
However ptr^ := Random(100); can also place 0 within the array which would terminate length function early. Unless you manage data so there is no null inside it is more "proper" to store the length within a separate variable or possibly at the beginning of array.
good point i didnt look at that but either way, you can just add a 1 to the random(100), and when you grab the value from the ptr you just -1, however, if the length is going to be below 255 bytes, just use the first byte to record the length of the data when it is written.

obviously you can also do larger by allocating upto 4 bytes... or even 8 bytes to record datalen

also TMemoryStream is a class, which will most likely be slower, classes are created on the heap (which is slower btw). also he asked how to check manually, using a memory stream would be cheating.

anyways the problem you will have (if you were do check without knowing the size from a header or via api), many files can contain terminators... lol (null chars), exe files for example.

-Colin
I am not make clear what is the point!
If it is a array, why not use the High(ByteArray) to get the length?
and, as I know, the null char is #0 not 0, I think maybe, there is difference
between #0 and 0., so, if you use Random(100), you may get 0, but you will not
get #0
if you just want to calc the file size when reading a file,
TFileStream is a good choice, just create and read the property 'size'.
If insisting calc the size by you self:
[pascal]
Var
Buffer : array[0..10*1024-1] of char;
i: integer;
totalsize : int64;

begin
totalsize :=0;
//create the TFileStream first....
repeat
i := theFileStream.Read(Buffer,10*1024-1);
totalsize := totalsize + i;
until i < 10*1024-1;
end;
[/pascal]