Hi
I'm exceeding a variable capacity somewhere in my code and I can't work out where. It must be to do with the 4 billion max capacity of an integer, but none of my variables in question are standard 32-bit integers.
Bascially, my program recurses a directory full of files and does stuff with each one. One of those things that it does is add it's size to the total amount of bytes already read from other files. So obviously if it's a directory full of many files that are many Gb's in size, the "Total Bytes Read" variable needs to be big.
The function I am using is from
http://stackoverflow.com/questions/1285979/delphi-function-to-display-number-of-bytes-as-windows-does and is reproduced below :
// This function converts the integer value of file size into human readable form
// Taken from:
// http://stackoverflow.com/questions/1285979/delphi-function-to-display-number-of-bytes-as-windows-does
function TForm1.FormatByteSize(const bytes: Longword): string;
var
B: byte;
KB: word;
MB: QWord;
GB: QWord;
TB: UInt64;
begin
B := 1; //byte
KB := 1024 * B; //kilobyte
MB := 1024 * KB; //megabyte
GB := 1024 * MB; //gigabyte
TB := 1024 * GB; //terabyte
if bytes > TB then
result := FormatFloat('#.## TiB', bytes / TB)
else
if bytes > GB then
result := FormatFloat('#.## GiB', bytes / GB)
else
if bytes > MB then
result := FormatFloat('#.## MiB', bytes / MB)
else
if bytes > KB then
result := FormatFloat('#.## KiB', bytes / KB)
else
result := FormatFloat('#.## bytes', bytes) ;
end;
Then there is a global variable called 'TotalBytesRead' that is accessed by two procedures :
=pascal]
public
{ public declarations }
FileCounter, NoOfFilesInDir2: integer;
TotalBytesRead : UInt64;
StopScan : Boolean;
The part of my code that calls FormatByteSize is here in a button click procedure. It is called for each file that is found so everything is initialised apart from TotalBytesRead, which is initialised just at the start of the program being launched :
var
SizeOfFile : int64; (needed for each individual file)
...
begin
FI := TFileIterator.Create;
SG := TStringGrid.Create(self);
SizeOfFile := 0; // (initialised to zero for each new file)
...
...
SizeOfFile := FileSize(File);
...
TotalBytesRead := TotalBytesRead + SizeOfFile;
edtTotalBytesExamined.Caption := FormatByteSize(TotalBytesRead);
My problem is that the value is edtTotalBytesExamined.Caption, when it exceeds 4Gb, it resets to zero, and I cannot work out why. Anyhelp appreciated as always