Lof() reports wrong file size for large files

From Liberty BASIC Family
Jump to navigation Jump to search

Description

lof() function reports the wrong file size for large files. Files greater than 4,294,967,296 bytes will be wrongly reported. This is due to the fact that the maximum number a 32bit long variable can hold is as above. Files greater than this 4.2Gb limit will cause the value to roll over, an 8Gb file will be reported as 3.4Gb.

The Files command can be use to obtain the correct file length.

Example code to demonstrate the bug.

print lof(#handle)

Example code to work around the bug.

'by tsh73

dim info$(10, 10)

'put your files here
'call checkSize "c:\testOut2.txt"   'small file, 37394 bytes - shows ok, match
'call checkSize "c:\test.dat"       '4 294 967 296
call checkSize "c:\test2.dat"      '8 618 733 815

end

sub checkSize fName$
  print "file: ";fName$
  print "Opening file and checking LOF"
  open  fName$ for input as #1
    print lof(#1)
  close #1
  print "Size by FILES command"
  aPath$=GetPath$(fName$)
  aName$=GetFilename$(fName$)
  files aPath$, aName$, info$()
  '    print "Found: ";info$(0, 0)
  print info$(1, 1)
  print
end sub

function GetFilename$(fileName$)
  i = len(fileName$)
  while mid$(fileName$, i, 1) <> "\" and mid$(fileName$, i, 1) <> ""
    i = i-1
  wend
  GetFilename$ = mid$(fileName$, i+1)
end function

function GetPath$(fileName$)
  i = len(fileName$)
  while mid$(fileName$, i, 1) <> "\" and mid$(fileName$, i, 1) <> ""
    i = i-1
  wend
  GetPath$ = left$(fileName$, i)
end function