关于文件流的读写(100分)

  • 主题发起人 主题发起人 寻路
  • 开始时间 开始时间

寻路

Unregistered / Unconfirmed
GUEST, unregistred user!
我自己写了两个读写文件流的函数,如下:
Function WriteSamFile(const Buffer;FilePath:string):Boolean;
var
sf:TFileStream;
begin
try
sf:=TFileStream.Create(FilePath,fmOpenWrite);
sf.Position := sf.Seek(0,soFromBeginning);
sf.Write(Buffer,sizeof(Buffer));
result :=True;
finally
sf.free;
sf:=nil;
end;
end;

Function ReadSamFile(var Buffer;FilePath:string):Boolean;
var
sf:TFileStream;
begin
try
sf:=TFileStream.Create(FilePath,fmOpenRead);
sf.Position :=Sf.Seek(0,soFromBeginning);
sf.Read(buffer,sizeof(buffer));
result := True;
finally
sf.Free;
sf:=nil;
end;
end;

然后,自定义了下面的数据结构:
type
MyType = record
x,y:Integer;
end;

TextSetup = record
Windows:MyType;
State:Boolean;
end;


再在ButtonBox控件的Chick事件中写代码,如下:
procedure TForm1.Button1Click(Sender: TObject);
var
test:TextSetup;
begin
test.State := True;
test.Windows.x := 2;
test.Windows.y := 4;
WriteSamFile(Test,'c:/test.Bin');
end;

procedure TForm1.Button2Click(Sender: TObject);
var
GetData:textsetup;
begin
Readsamfile(GetData,'c:/test.sam');
showmessage('x:' + inttostr(GetData.Windows.x) + ' y:'
+ Inttostr(GetData.Windows.y));
end;

为什么我读出的数据和我写入文件的数据不同?
 
最后面的
Readsamfile(GetData,'c:/test.sam');
写错了,应该是:
Readsamfile(GetData,'c:/test.Bin');
 
DELPHI6+WIN2000测试通过。
Function WriteSamFile(const Buffer;WriteLen:integer;FilePath:string):Boolean;
var
sf:TFileStream;
begin
try
sf:=TFileStream.Create(FilePath,fmOpenWrite);
sf.Position := sf.Seek(0,soFromBeginning);
sf.Write(Buffer,WriteLen);
result :=True;
finally
sf.free;
sf:=nil;
end;
end;

Function ReadSamFile(var Buffer;ReadLen:integer;FilePath:string):Boolean;
var
sf:TFileStream;
begin
try
sf:=TFileStream.Create(FilePath,fmOpenRead);
sf.Position :=Sf.Seek(0,soFromBeginning);
sf.Read(buffer,ReadLen);
result := True;
finally
sf.Free;
sf:=nil;
end;
end;

type
MyType = record
x,y:Integer;
end;

TextSetup = record
Windows:MyType;
State:Boolean;
end;




procedure TForm1.Button1Click(Sender: TObject);
var
test:TextSetup;
begin
test.State := True;
test.Windows.x := 200;
test.Windows.y := 400;
WriteSamFile(Test,sizeof(TextSetup),'c:/temp/test.Bin');
end;

procedure TForm1.Button2Click(Sender: TObject);
var
GetData:textsetup;
begin

Readsamfile(GetData,sizeof(textsetup),'c:/temp/test.Bin');
showmessage('x:' + inttostr(GetData.Windows.x) + ' y:'
+ Inttostr(GetData.Windows.y));
end;


 
To:gonghh
在 ReadSamFile 和 WriteSamFile 函数当中,我用 sf.Read(buffer,sizeof(buffer));
为什么不能得到 Buffer 结构的大小?
 
接受答案了.
 
后退
顶部