FileRead (Delphi)
Description
The following example uses a button, a string grid, and an Open dialog box on a form. When the button is clicked, you are prompted for a file name. When you click OK, the specified file is opened, read into a buffer, and closed. Then the buffer is displayed in two columns of the string grid. The first column contains the character values in the buffer. The second column contains the numeric values of the characters in the buffer.
Code
procedure TForm1.Button1Click(Sender: TObject);
var
iFileHandle: Integer;
iFileLength: Integer;
iBytesRead: Integer;
Buffer: PAnsiChar;
i: Integer;
begin
if OpenDialog1.Execute then
begin
try
iFileHandle := SysUtils.FileOpen(OpenDialog1.FileName, fmOpenRead);
iFileLength := SysUtils.FileSeek(iFileHandle,0,2);
FileSeek(iFileHandle,0,0);
Buffer := System.AllocMem(iFileLength + 1);
iBytesRead := SysUtils.FileRead(iFileHandle, Buffer^, iFileLength);
FileClose(iFileHandle);
for i := 0 to iBytesRead-1 do
begin
StringGrid1.RowCount := StringGrid1.RowCount + 1;
StringGrid1.Cells[1,i+1] := Buffer[i];
StringGrid1.Cells[2,i+1] := IntToStr(Integer(Buffer[i]));
end;
finally
FreeMem(Buffer);
end;
end;
end;
Uses
- System.SysUtils.FileOpen ( fr | de | ja )
- System.SysUtils.FileSeek ( fr | de | ja )
- System.SysUtils.FileRead ( fr | de | ja )
- System.AllocMem ( fr | de | ja )
- System.FreeMem ( fr | de | ja )