• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 154
  • Last Modified:

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 ?
0
borgo
Asked:
borgo
1 Solution
 
edeyCommented:
Delete questions are read.

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

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

Peter
0
 
mscatenaCommented:
You may try the following. It's not the fastest way (BlockRead is faster) but it's simple:

var
  Arq: file of char;
  Buffer: array[0..4] of char;
  i: integer;
begin
  AssignFile(Arq, 'filename.ext');
  Reset(Arq);
  try
    seek(Arq, 10 - 1);
    for i := 0 to 4 do
      read(Arq, Buffer[i]);
  finally
    CloseFile(Arq);
  end;
  // Now the data read is at Buffer
end;
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
kretzschmarCommented:
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
0
 
intheCommented:
listening..
0
 
MotazCommented:
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
0
 
kretzschmarCommented:
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
0
 
edeyCommented:
I suppose, while we are processing enumAllPossibleSolutions(exampleCallBackProc) :) one *could* also try this with memory mapped files :).



Gl
Mike
0
 
kretzschmarCommented:
yup, thats missed ;-)

sample please
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now