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 ?
borgoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.