Einzelnen Beitrag anzeigen

Klaus01

Registriert seit: 30. Nov 2005
Ort: München
5.755 Beiträge
 
Delphi 10.4 Sydney
 
#5

AW: FileStream von Byte x bis Byte y lesen und als String ausgeben.

  Alt 20. Nov 2020, 20:58
..versuche es mal so:
Delphi-Quellcode:
program Project1;

{$APPTYPE CONSOLE}
{$R *.res}

uses System.SysUtils, System.Classes, System.IoUtils;

const
   filePath = 'u:\Test.dat';
   startByte = 33;
   byteCount = 4; // 33, 34, 35, 36
var
   fileStream: TStream;
   myBytes: Array[0..byteCount -1] of Byte;
   myString: String;
   bytesRead: Integer;
begin
   fileStream := TFile.Open(filePath, TFileMode.fmOpen);
   try
      if fileStream.Seek(startByte, TSeekOrigin.soBeginning) < startByte then
         raise Exception.Create('file is too small');

      if fileStream.Read(myBytes, byteCount) < byteCount then
         raise Exception.Create('file is too small');

      myString := TEncoding.ASCII.GetString(myBytes);
   finally
      fileStream.Destroy();
   end;
end.
Grüße
Klaus
Klaus
  Mit Zitat antworten Zitat