Your code does not use Stream reader, and it's entirely unlikely that the issue is with reading the file. Here's an example of using TFileStream
to both read and write your record successfully without altering the values in any way. (Clearly you'll have to change the TestFile
constant to point to a valid, writable location on your system first.)
program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils, Classes;
type
TMyHeader = packed record
Value1: String[20];
Value2: Word;
Value3: Word;
Value4: Word;
end;
var
MyHeader: TMyHeader;
Stream: TFileStream;
const
TestFile = 'E:\TempFiles\Testheader.bin';
begin
// Initialize the record
MyHeader.Value1 := 'Testing One Two';
MyHeader.Value2 := 43140;
MyHeader.Value3 := 12345;
MyHeader.Value4 := 43140;
// Show what it contains. The `[]` brackets are to clearly delimit Value1
WriteLn(Format('Before write: [%s] %d %d %d',
[MyHeader.Value1, MyHeader.Value2, MyHeader.Value3, MyHeader.Value4]));
// Write it to the stream.
Stream := TFileStream.Create(TestFile, fmCreate, fmShareDenyNone);
try
Stream.Write(MyHeader, SizeOf(TMyHeader));
finally
Stream.Free;
end;
// Clear the contents of MyHeader before reading back, and show the empty values
FillChar(MyHeader, SizeOf(TMyHeader), 0);
WriteLn(Format('Before read: [%s] %d %d %d',
[MyHeader.Value1, MyHeader.Value2, MyHeader.Value3, MyHeader.Value4]));
Stream := TFileStream.Create(TestFile, fmOpenRead, fmShareDenyNone);
try
Stream.Read(MyHeader, SizeOf(TMyHeader));
finally
Stream.Free;
end;
// Output what we've read back in to verify it is correct
WriteLn(Format('After read: [%s] %d %d %d',
[MyHeader.Value1, MyHeader.Value2, MyHeader.Value3, MyHeader.Value4]));
Readln;
end.