It lives!
program read_binary;
uses SysUtils;
type
THeader = record
Serial: Word; //2 bytes
Filename: String[255]; //1 byte
Date: Word; //2 bytes
Fieldnrs: Word; //2 bytes
end;
TFields = record
Fieldnr: Word; //2 bytes
Fieldname: String[255]; //1 byte
end;
var
BinaryStream: File;
Header: THeader;
Fields: array [0..10] of TFields;
nrs: Integer;
procedure myRead(var Buffer; Size: Integer);
begin
BlockRead(BinaryStream, Buffer, Size);
end;
begin
assign(BinaryStream, 'C:\Dev-Pas\EXAMEN2.dat');
reset(BinaryStream, 1);
myRead(Header.Serial, 2);
myRead(Header.Filename[0], 1);
myRead(Header.Filename[1], ord(Header.Filename[0]));
myRead(Header.Date, 2);
myRead(Header.Fieldnrs, 2);
Header.Serial := Swap(Header.Serial);
Header.Fieldnrs := Swap(Header.Fieldnrs);
//SetLength (Fields, Header.Fieldnrs);
for nrs := 0 to Header.Fieldnrs - 1 do
begin
myRead (Fields[nrs].Fieldnr, 2);
Fields[nrs].Fieldnr := Swap (Fields [nrs].Fieldnr);
myRead (Fields[nrs].Fieldname[0],1);
myRead (Fields[nrs].Fieldname[1], ord(Fields[nrs].Fieldname[0]));
end;
Writeln ('Serial: ', Header.Serial);
Writeln ('Filename: ', Header.Filename);
Writeln ('Date: ', Header.Date);
Writeln ('Fieldnrs: ',Header.Fieldnrs);
for nrs := 0 to Header.Fieldnrs - 1 do
begin
Writeln ('Campo [codigo: ',Fields[nrs].Fieldnr,', ','descripcion: ',Fields[nrs].Fieldname ,']' );
end;
Close(BinaryStream);
ReadLn;
end.
got something wrong with the date.
Now I have to show the Field according to its number. and the FieldData.
the crappy thing is: I have to update the date with the current system's YYYY/MM/DD but SysUtils's got GetDate. I could use that?
edit: modified how to show example data.
If I remove the SWAP the program shuts down (like if there was no ReadLn).