delphi文件读写问题 从某个指定的位置写入却把之前的内容清除了
代码里哪里出了问题
或者应该用何种方式读写呢?
还有代码中注释掉的填f的循环怎么不会中断了?
procedure TForm1.Button6Click(Sender: TObject);
begin
  if (edit1.Text='') and (edit2.Text='') and (edit3.Text='') and (edit4.Text='') then
    begin
      MessageBox(Handle, 'Load File First!!', '提示', MB_ICONEXCLAMATION);
      Exit;
    end;
  if  edit1.Text<>'' then
    begin
    try
      f:='f';
      fs1:=TMemoryStream.Create;
      fs1.LoadFromFile(edit1.Text);
      ts1:=TFileStream.Create('c:\image.bin',fmOpenWrite or fmCreate);
      ts1.CopyFrom(fs1,0);
      ts1.Seek(0,soFromEnd);
      p1:=ts1.Position;
      ShowMessage(IntToStr(p1));
        while p1<$20000 do
          ts1.Write(f,sizeOf(f));
          p1:=ts1.Seek(0,soFromEnd);
          exit;
      finally
      showmessage(IntToStr(p1));
      ShowMessage('f1.bin OK!');
      //finally
      fs1.Free;
      ts1.Free;
      end;
    end;  if edit2.Text<>'' then
    begin
    try
      fs2:=TMemoryStream.Create;
      fs2.LoadFromFile(edit2.Text);
      ts2:=TFileStream.Create('c:\image.bin',fmOpenWrite or fmCreate);
      ts2.Position:=$20001;
      ts2.CopyFrom(fs2,0);
      ShowMessage('f2.b OK!');
    finally
      fs2.Free;
      ts2.Free;
    end;
  end;

解决方案 »

  1.   

    填f的循环改成现在这样貌似可以了?for p1:=ts1.Position to $20000 do
              ts1.Write(f,sizeOf(f));
              p1:=ts1.Seek(0,soFromEnd);
          finally
      

  2.   

    ts1:=TFileStream.Create('c:\image.bin',fmOpenReadWrite);//fmCreate
    ts1.Position:=$100;
    ts1.Write(f,sizeOf(f));
      

  3.   


    我后来自己折腾出来了
    后面几个去掉了fmOpenCreate就可以了问一句 ts1.Write(f,sizeOf(f)); 的sizeof(f)换成1可以么
      

  4.   

    去掉fmCreate看f是什么数据类型了,如果是ansi的字符1就可以,如果是宽字符就不成。
      

  5.   

    我这样定义的 你看行么f:Integer;
    f:=$f;
      

  6.   

    ts2:=TFileStream.Create('c:\image.bin',fmOpenWrite or fmCreate);
    这样写还不如先判断一次文件存不存在
      

  7.   

    还有个问题 接在上一次写完后在尾部继续写 是‘position’ 还是‘position+$1'呢