Link to home
Start Free TrialLog in
Avatar of borgo
borgo

asked on

Reading Files

Hi Expert
For example I'd like to read a file from the byte 10 to the byte 15.
How i can do it ?
Thank you.

PS: Did you see the new look of ExpertExchange ? Why some rows are red ?
Avatar of edey
edey

Delete questions are read.

To read specific parts of a file, one would use the blockread function

Gl
Mike
Or you should consider to use :
FileOpen, FileSeek, FileRead and FileClose.

Peter
ASKER CERTIFIED SOLUTION
Avatar of mscatena
mscatena

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kretzschmar
hi all,

i would prefer to use a TFilestream
-with the position-property you can easy position the 'filecursor'
-with the method readbuffer you can easy copy a defined amount of bytes at position to the buffer

>Did you see the new look of ExpertExchange ? Why some rows are red ?
they are red, because the questioner has deleted the q, if another expert will pevent this, then the expert posts a comment on it and the q is live again, if no comment is added the q will disappear after a specific time.

meikl
listening..
try this:

procedure ReadFrom10To15(FileName: string; var Buf: array of Byte);
var
  F: File;
begin
  AssignFile(F, FileName);
  FileMode:= 0; // Read only
  Reset(F, 1);
  Seek(F, 10);
  BlockRead(F, Buf, 6);
  CloseFile(F);
end;

Comment:

This will read from byte 10 (11'th byte), because the first record is number 0, so that if you want to read from the 10'th byte you must change seek procedure to:

  Seek(F, 9);

Motaz

http://www.geocities.com/motaz1
hi,

seemed i have to give a sample too

procedure GetPartFromFile(FileName : String; FromPos, Size : Integer; var Buffer);
var F : TFileStream;
Begin
  try
    F := TFileStream.Create(FileName, fmOpenRead);
    try
      F.Position := FromPos;
      F.ReadBuffer(Buffer,Size);
    finally
      F.Free;
    end;
  except
    Raise;  //forward exception
  end;
end;

//how to call
procedure TForm1.Button1Click(Sender: TObject);
var MyBuf : Array[0..4] of Byte;
begin  //get bytes 11,12,13,14,15
  GetPartFromFile('c:\test.txt',10,SizeOf(MyBuf),MyBuf);
  showmessage(  //HexOutput
     Format('%x %x %x %x %x',[MyBuf[0],MyBuf[1],MyBuf[2],MyBuf[3],MyBuf[4]]));

end;

meikl
I suppose, while we are processing enumAllPossibleSolutions(exampleCallBackProc) :) one *could* also try this with memory mapped files :).



Gl
Mike
yup, thats missed ;-)

sample please