[DUG] Reading Different Types from a Binary File
Eric A
eaa603 at hotmail.com
Mon Nov 5 18:35:57 NZDT 2012
Thanks, Ross.
Yes, I am in fact using TFileStream and its Read method for the binary extraction.
For the Header, I was hoping to find an existing method that I could call to to extract the the CR LF terminated strings, however I will use your suggestion for the header fields rather than indexing the char buffer.
From: ross at stationplaylist.com
To: delphi at listserver.123.net.nz
Date: Mon, 5 Nov 2012 17:28:12 +1300
Subject: Re: [DUG] Reading Different Types from a Binary File
Personally I would use a TFileStream and FS.Read(S,1) one AnsiChar at a time, concatenating into an AnsiString if you want the info until you have ‘[End]’#13#10 in the string. This will have the file pointer at the start of the binary data where you can then FS.Read(MyDWORD,4) or MyByte,1 etc. Ross. From: delphi-bounces at listserver.123.net.nz [mailto:delphi-bounces at listserver.123.net.nz] On Behalf Of Eric A
Sent: Monday, 5 November 2012 9:08 a.m.
To: Delphi ListServer
Subject: [DUG] Reading Different Types from a Binary File This is probably a no-brainer to most of the DUG members, especially those used to pointers ...
I need to read and extract data from a binary file which comprises:
- a header with character type strings, each terminated by CR LF (not nulls)
- binary data area with a mixture of byte types, word types and double word types
- a file terminator of 0xFFFF hex
The files are not huge, less than 500KB, as the structure originates from old memory-constrained microprocessors of the 80's.
The header looks like it is based on the old INI file format and is of the form:
[Header]
Created=1349233072
Title=
Type=D
Version=162
[End]
The binary data area follows next. It comprises different types of data blocks, with sub blocks, each of which contains either bytes, words or double words of data in a pre-defined pattern or structure depending on the type of data block.
I can read the file into a buffer (array of Char) but I currently have a chunk of somewhat ugly code to parse the header and find the start position of the binary data after the '[End] CR LF' string. After that I need to extract data of various types depending on the specified datablock types, which I though I might have to use pointers for.
What's the best approach for
a) Extracting the header strings - is there a Delphi function to extract CR LF terminated character sequences as strings and maintain a pointer to the end of the string?
b) Creating functions for ReadByte, ReadWord, ReadDoubleWord and incrementing the pointer appropriately in each case ?
Eric
_______________________________________________
NZ Borland Developers Group - Delphi mailing list
Post: delphi at listserver.123.net.nz
Admin: http://delphi.org.nz/mailman/listinfo/delphi
Unsubscribe: send an email to delphi-request at listserver.123.net.nz with Subject: unsubscribe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://listserver.123.net.nz/pipermail/delphi/attachments/20121105/cbcf7f2b/attachment.html
More information about the Delphi
mailing list