Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Percent of file goes wrong


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 1 - Last Post: Mar 1, 2018 11:48 AM Last Post By: Remy Lebeau (Te...
Donald Bossen

Posts: 81
Registered: 2/3/02
Percent of file goes wrong  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 1, 2018 11:24 AM
Some Thing is wrong whit this simple Percent reading.

private
TotalBitsRead : Cardinal;
TotalFileBytes : Int64;

procedure TFrmMain.BtnImportClick(Sender: TObject);
var
Total,rep : Cardinal;
cnt : integer;
x,row : integer;
FldCnt : Integer;
ImportFile : TextFile;
fName : String;
Line : String;
O,R : String;
List : TStringList;
begin
fName := FldImportFile.Text;
if FileExists(fName) then
begin
TotalFileBytes := FileSizeBytes(fName);
AssignFile(ImportFile, fName);
FileMode := 0;
Reset(ImportFile);
try
TotalBitsRead := 0;
FldStatus.Caption := 'Processing List';
Application.ProcessMessages;
List := TStringList.Create;
try
row := 1;
Line := '';
FldImportData.BeginUpdate;

while not Eof(ImportFile) do
begin
ReadLn(ImportFile,Line);
CalcSteps(TotalFileBytes,Line);
end;
FldImportData.EndUpdate;

finally
List.Free;
end;
finally
CloseFile(ImportFile);
end;
LastStep(Total);
Showmessage('Process Complete!');
end
else
begin
ShowMessage('File Dir or Name does not exist');
FldImportFile.SetFocus;
end;
end;

function TFrmMain.FileSizeBytes(FileName : String) : Int64;
var
Rec : TSearchRec;
begin
Result := 0;
if (FindFirst(FileName, faAnyFile, Rec) = 0) then
begin
Result := Rec.Size;
FindClose(Rec);
end;
end;

procedure TFrmMain.CalcSteps(TotalBytes : int64; Data : String);
var
bitsRead : int64;
begin
bitsRead := sizeOf(Char) * (Length(data)+1);
TotalBitsRead := TotalBitsRead + BitsRead;
FldProgress.Percent := round(100 *(TotalBitsRead / TotalFileBytes));
FldStatus.Caption := 'Total Byte = '+IntToStr(TotalBytes)+' Byte Read = '+IntToStr(TotalBitsRead);
application.ProcessMessages;
end;
Some How the percent does not calculate as the Lines are read in its all over the place
Thanks
Donald S. Bossen

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Percent of file goes wrong  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 1, 2018 11:48 AM   in response to: Donald Bossen in response to: Donald Bossen
Donald Bossen wrote:

Some Thing is wrong whit this simple Percent reading.

Why are you using old-style file I/O, instead of something more modern
(like TStreamReader)?

bitsRead := sizeOf(Char) * (Length(data)+1);
TotalBitsRead := TotalBitsRead + BitsRead;

Why +1? For the line break? What if the file is using CRLF instead of
bare-CR or bare-LF for line breaks?

For a more accurate count, you would have to keep track of the
underlying file's actual position after each read. The number of
chars in the line is not necessarily the same as the number of
bytes read, especially in a Unicode environment where SizeOf(Char) <>
SizeOf(Byte).

Also, your calculation is counting bytes, not bits. If you want
bits, you need to multiply the result by 8 (8 bits in a byte).

FldProgress.Percent := round(100 *(TotalBitsRead / TotalFileBytes));

You should be using the TotalBytes parameter of your function, instead
of using the TotalFileBytes member directly. Otherwise, there is no
point in having the TotalBytes parameter at all.

Some How the percent does not calculate as the Lines are read in its
all over the place

I don't understand what you are saying. Please clarify with a real
example.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02