Mega Code Archive

 
Categories / Delphi / Examples
 

Read and blockread file function failure in 32bit

Question: When I port my code from 16 bit Delphi to 32 bit Delphi, the read and BlockRead function no longer seem to work correctly. Is this a bug? Answer: No. In 32 bit environments, records are automatically aligned on 32 bit boundaries when the $A+ compiler directive is in effect. This is done by padding the record so each record will be located on a 32 bit boundary. If you are reading in data from a file containing non-aligned records, the data may not be read in correctly if you are using automatic record alignment. To turn off automatic record alignment for a given record type, use the "packed" record attribute. Globally turning off record alignment with the $A- is not recommended, as much of the system requires record alignment to be on. The following example demonstrates the size difference of a packed and unpacked record. Example: type TSomeRec = record w1 : word; b1 : byte; w2 : word; end; type TSomePackedRec = packed record w1 : word; b1 : byte; w2 : word; end; procedure TForm1.Button1Click(Sender: TObject); var r : TSomeRec; s : TSomePackedRec; begin ShowMessage(IntToStr(SizeOf(r))); {displays 6} ShowMessage(IntToStr(SizeOf(s))); {displays 5}; end;