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;
代码里哪里出了问题
或者应该用何种方式读写呢?
还有代码中注释掉的填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;
ts1.Write(f,sizeOf(f));
p1:=ts1.Seek(0,soFromEnd);
finally
ts1.Position:=$100;
ts1.Write(f,sizeOf(f));
我后来自己折腾出来了
后面几个去掉了fmOpenCreate就可以了问一句 ts1.Write(f,sizeOf(f)); 的sizeof(f)换成1可以么
f:=$f;
这样写还不如先判断一次文件存不存在