Примеры использования TFileStream для чтения файлов

procedure TForm1.Button1Click(Sender: TObject);
var
  Buffer: PChar;
  Stream: TFileStream;
  Size:   LongInt;
begin
  Stream := TFileStream.Create('Unit1.pas', fmOpenRead);
  try
    Size := Stream.Size;
    GetMem(Buffer, Size);
    try
      Stream.Read(Buffer[0], Size);
      Memo1.Lines.Text := Buffer;
    finally
      FreeMem(Buffer);
    end;
  finally
    Stream.Free;
  end;
end;

или не используя управление памятью:

procedure TForm1.Button2Click(Sender: TObject);
var
  Buffer: string;
  Stream: TFileStream;
begin
  Stream := TFileStream.Create('unit1.pas', fmOpenRead);
  try
    SetLength(buffer, Stream.Size);
    Stream.Read(Buffer[1], Stream.Size);
    Memo1.Lines.Text := Buffer;
  finally
    Stream.Free;
  end;
end;
 
faq/delphi/system/reading_from_file_tfilestream.txt · Последнее изменение: d.m.Y H:i — 127.0.0.1
 
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki