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 ?
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 ?
Or you should consider to use :
FileOpen, FileSeek, FileRead and FileClose.
Peter
FileOpen, FileSeek, FileRead and FileClose.
Peter
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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(FileNam e, 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.t xt',10,Siz eOf(MyBuf) ,MyBuf);
showmessage( //HexOutput
Format('%x %x %x %x %x',[MyBuf[0],MyBuf[1],MyB uf[2],MyBu f[3],MyBuf [4]]));
end;
meikl
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(FileNam
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
var MyBuf : Array[0..4] of Byte;
begin //get bytes 11,12,13,14,15
GetPartFromFile('c:\test.t
showmessage( //HexOutput
Format('%x %x %x %x %x',[MyBuf[0],MyBuf[1],MyB
end;
meikl
I suppose, while we are processing enumAllPossibleSolutions(e xampleCall BackProc) :) one *could* also try this with memory mapped files :).
Gl
Mike
Gl
Mike
yup, thats missed ;-)
sample please
sample please
To read specific parts of a file, one would use the blockread function
Gl
Mike