Board index » delphi » Reading binary files using TFileStream
SamFelis
Delphi Developer |
Reading binary files using TFileStream2005-12-01 03:24:55 AM delphi138 Using D7, I am trying to read a binary file that I (sort of) know the structure to using TFileStream. The structure is as follows: {struct file { char hdr[64]; int nruns; local int i = 0; for (i=0; i<nruns;i++) { struct run runs; } } struct run { int nlaps; struct lap laps[nlaps]; char dunno[7]; short ntracks; char dunno2[3]; struct trackpoint tp[ntracks]; }; struct trackpoint { unsigned int x; unsigned int y; double alt; char char1; time_t date; char zeros[19]; }; struct lap { time_t start; int dur; struct posns { short s1; unsigned int px; unsigned int py; char zeroes[8]; } pos[2]; float len; int cal; }; So, what I am trying to do is read the first 64 bytes of the file and then read the next four to determine the number of runs. I will then read the four to get the number of laps for the first run and so on and so forth until the data for the first run is logged. I will then loop and do it again. The problem I am having is actually reading the data. Whenever I try to access the header information, I get an access violation (see snippet below). (I haven't even tried to get to the number of runs.) var binStream: TFileStream; binString: string; iHeadLength : integer; sHeader : Word; begin try //stream from the binary file binStream := TFileStream.Create('c:\logbook.bin', fmOpenRead or fmShareDenyNone); try binStream.Position := 0; binStream.ReadBuffer(sHeader, 64); ShowMessage(PChar(sHeader)); finally binStream.Free; end; except on e : Exception do begin ShowMessage(e.Message); end; end; end; I've searched high and low for examples of how to do something like this, but haven't found anything that correlates to what I am trying to do. Any suggestions? |