S
sxwy
Unregistered / Unconfirmed
GUEST, unregistred user!
我想把一些数据写成一个二进制文件,如下:
第一种方式固定数组:
var f: TFileStream;
FS : file;
fbuffer:array[0..1] of byte;
begin
Savedialog1.DefaultExt := 'V1';
Savedialog1.Filter := '*.V1';
if Savedialog1.Execute then
begin
Assignfile(FS,Savedialog1.FileName);
Rewrite(FS);
Closefile(FS);
end;
fbuffer[0] := 12;
f:= TFileStream.Create(Savedialog1.FileName,fmOpenReadWrite);
f.Seek(0,soFromCurrent);
f.Write(fBuffer,sizeof(fbuffer));
f.Free;
第二种方式动态数组:
var f: TFileStream;
FS : file;
fbuffer:array of byte;
begin
Savedialog1.DefaultExt := 'V1';
Savedialog1.Filter := '*.V1';
if Savedialog1.Execute then
begin
Assignfile(FS,Savedialog1.FileName);
Rewrite(FS);
Closefile(FS);
end;
setlength(fbuffer,1);
fbuffer[0] := 12;
f:= TFileStream.Create(Savedialog1.FileName,fmOpenReadWrite);
f.Seek(0,soFromCurrent);
f.Write(fBuffer,sizeof(fbuffer));
f.Free;
end;
以上两种方式中,第一种能看到写入的直实数据,第二种写入的数据完全不对了.
请问这是什么原因呢.
第一种方式固定数组:
var f: TFileStream;
FS : file;
fbuffer:array[0..1] of byte;
begin
Savedialog1.DefaultExt := 'V1';
Savedialog1.Filter := '*.V1';
if Savedialog1.Execute then
begin
Assignfile(FS,Savedialog1.FileName);
Rewrite(FS);
Closefile(FS);
end;
fbuffer[0] := 12;
f:= TFileStream.Create(Savedialog1.FileName,fmOpenReadWrite);
f.Seek(0,soFromCurrent);
f.Write(fBuffer,sizeof(fbuffer));
f.Free;
第二种方式动态数组:
var f: TFileStream;
FS : file;
fbuffer:array of byte;
begin
Savedialog1.DefaultExt := 'V1';
Savedialog1.Filter := '*.V1';
if Savedialog1.Execute then
begin
Assignfile(FS,Savedialog1.FileName);
Rewrite(FS);
Closefile(FS);
end;
setlength(fbuffer,1);
fbuffer[0] := 12;
f:= TFileStream.Create(Savedialog1.FileName,fmOpenReadWrite);
f.Seek(0,soFromCurrent);
f.Write(fBuffer,sizeof(fbuffer));
f.Free;
end;
以上两种方式中,第一种能看到写入的直实数据,第二种写入的数据完全不对了.
请问这是什么原因呢.