Search code examples
linuxfilefilestreamfilesizelazarus

Linux Lazarus: Wrong FileSize Reported by TFileStream


I am trying to read my file through Filestream to send it over the network and I have noticed something odd. I am not sure why. My actual filesize is 44.7KB, but when filestream reads the same file it tells me that the filesize is 45228 Bytes or 45.2 KB. Why is that? Is there is a way to fix that?

fs:TFileStream;

fs := TFileStream.Create('myfile.dat', fmOpenRead or fmshareDenyWrite);

showmessage(inttostr(fs.Size));

Solution

  • One possibility is that whatever you are using to report the file size is measuring the size using kibibytes (1024 bytes) rather than kilobytes (1000 bytes).

    Divide 45228 by 1024 to get 44.2KiB. This still doesn't match exactly but I would not be surprised if there was a transcription error in your question. There is at least one, where you wrote FileSize rather than Size (now corrected by a question edit) so my guess is that some of the other specific details are incorrect.

    Other than that I think it very likely that the problem is in your other method of obtaining the file size. TFileStream.Size can be trusted to give an accurate value. If that doesn't tally with some other measure then that other measure is probably wrong.

    On Linux you can use the stat command to get a definitive report of the file size. I would expect that to yield the same value as TFileStream.Size.