将一个字符串写入一个二进制文件的任意位置,并要能读出来?请给出例子 (100分)

  • 主题发起人 主题发起人 小生
  • 开始时间 开始时间

小生

Unregistered / Unconfirmed
GUEST, unregistred user!
我要将一个字符串写入一个二进制文件的任意位置,并要能读出来,总是读出来的东东不对,<br>且在button1上按回车键与按空格键 返回值不一样。<br>(aaa.dat 请任找一个文件 窗口放置 edit1 edit2 button1)<br><br>procedure TForm1.Button1Click(Sender:TObject);<br>var<br>&nbsp; FileName, text, str:string;<br>&nbsp; F:TFileStream;<br>begin<br>&nbsp; text:=edit1.text;<br>&nbsp; FileName:='E:/aaa.dat';<br>&nbsp; try//写<br>&nbsp; &nbsp; F:=TFileStream.create(filename, fmOpenReadWrite);<br><br>&nbsp; &nbsp; str:=copy(text, 1, 2);<br>&nbsp; &nbsp; F.seek($10, 0); //0为从文件头 1为当前位置 2为文件尾<br>&nbsp; &nbsp; F.Write(str, 2);<br><br>&nbsp; &nbsp; str:=copy(text, 3, 2);<br>&nbsp; &nbsp; f.Seek($02, 1);<br>&nbsp; &nbsp; F.write(str, 2);<br>&nbsp; finally<br>&nbsp; &nbsp; F.free;<br>&nbsp; end;<br>&nbsp; try//读<br>&nbsp; &nbsp; f:=TFileStream.Create(filename, fmOpenReadWrite);<br>&nbsp; &nbsp; SetLength(str, f.size);<br>&nbsp; &nbsp; F.seek($10, 0);<br>&nbsp; &nbsp; f.Read(str[1], 2);<br>&nbsp; &nbsp; edit2.Text:=str;<br>&nbsp; &nbsp; f.seek($02, 1);<br>&nbsp; &nbsp; f.Read(str[1], 2);<br>&nbsp; &nbsp; edit2.Text:=edit2.Text+str;<br>&nbsp; finally<br>&nbsp; &nbsp; f.free;<br>&nbsp; end;<br>end;<br>
 
两个 &nbsp; &nbsp; F.Write(str, 2);<br>都改与如下:<br>&nbsp; &nbsp; F.Write(str[1], 2);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;///<br><br>
 
写的时候这样<br>var <br>&nbsp; fs: TFileStream;<br>&nbsp; t: Integer;<br>&nbsp; S: String;<br><br>....................<br>fs.Position := 1580;<br>t := Length(S);<br>fs.Write(t, SizeOf(t));<br>fs.Write(PChar(S)^, t);<br><br>读的时候<br>fs.Position := 1580;<br>fs.Read(t, SizeOf(t));<br>SetLength(S, t);<br>fs.Read(PChar(S)^, t);<br><br>ok,搞定<br>
 
function StrWrite(FileName:string; Sn:string; StartByte:integer; SpaceByte:integer):string;<br>var<br>&nbsp; i:integer;<br>&nbsp; b:Byte;<br>&nbsp; F:TFileStream;<br>begin<br>&nbsp; b:=Ord(sn[1]);<br>&nbsp; try<br>&nbsp; &nbsp; F:=TFileStream.Create(FileName, fmOpenReadWrite);<br>&nbsp; &nbsp; F.Seek(StartByte, soFromBeginning);<br>&nbsp; &nbsp; F.Write(b, 1);<br>&nbsp; &nbsp; for i:=2 to length(sn) do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; b:=Ord(sn);<br>&nbsp; &nbsp; &nbsp; F.Seek(SpaceByte, 1);<br>&nbsp; &nbsp; &nbsp; F.Write(b, 1);<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; F.Free;<br>&nbsp; end;<br>end;<br><br>function StrRead(FileName:string; StartByte:integer; SpaceByte:integer; SnLength:integer):string;<br>var<br>&nbsp; i:integer;<br>&nbsp; b:Byte;<br>&nbsp; F:TFileStream;<br>begin<br>&nbsp; try<br>&nbsp; &nbsp; F:=TFIleStream.Create(FileName, fmOpenReadWrite);<br>&nbsp; &nbsp; F.Seek(StartByte, soFromBeginning);<br>&nbsp; &nbsp; F.Read(b, 1);<br>&nbsp; &nbsp; Result:=chr(b);<br>&nbsp; &nbsp; for i:=2 to SnLength do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; F.Seek(SpaceByte, 1);<br>&nbsp; &nbsp; &nbsp; F.Read(b, 1);<br>&nbsp; &nbsp; &nbsp; Result:=Result+chr(b);<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; F.Free;<br>&nbsp; end;<br>end;<br>
 
多人接受答案了。
 
后退
顶部