This code is running on Delphi XE4 perfectly well:
var
b: byte;
fl: TFileStream;
filename:string;
begin
b:= $2F;
filename:='C:\test.exe';
fl:= tFileStream.Create(filename, 0,fmOpenReadWrite);
fl.Position:= $C;
fl.WriteBuffer(b,sizeof(b));
fl.free
end;
However, when I run exactly the same code on Delphi XE7 on the same PC, it fails with the error "Stream write error".
In the
TFileStream
constructor, you are setting theMode
parameter to 0 (fmOpenRead
) and theRights
parameter tofmOpenReadWrite
. You need to swap them:Or simply:
When the
fmCreate
flag is not present in theMode
parameter,TFileStream
callsFileOpen()
instead ofFileCreate()
.In XE4, the
Mode
andRights
parameters areOR
'ed together whenTFileStream
callsFileOpen()
on Windows:That is why your code works in XE4. You are opening the file in a read/write mode.
In XE7, the
Rights
parameter is ignored whenTFileStream
callsFileOpen()
on every platform:That is why your code does not work in XE7. You are opening the file in a read-only mode.