一个将MemoryStream写到FileStream的奇怪问题 (30分)

  • 主题发起人 主题发起人 ie
  • 开始时间 开始时间
I

ie

Unregistered / Unconfirmed
GUEST, unregistred user!
我将内存中的数据写到文件中,
l_MemoryStream := TMemoryStream.create;
l_FileStream := TFileStream.Create('c:/tmp/aaa', fmOpenWrite + fmShareDenyNone);
l_FileStream.seek(0, soFrombegin
ning);
l_MemoryStream.SaveToStream(l_FileStream);
这样是不是将l_FileStream中的内容覆盖掉吗,怎么变成增加到文件中,也就是说aaa的文件变大了
增加的大小是l_MemoryStream的大小,请问,我要如何写才是覆盖原来的文件内容。
我看了帮助,好象也是说这样做是增加内容,不知道理解的对不对,
Use SaveToStream to copy data that is stored in memory into another storage medium
. SaveToStream writes the entire contents of Memory into the indicated stream
object, starting at the current position in the stream that was passed as
a parameter.
When the Stream parameter is a TFileStream object,
SaveToStream do
es much the same thing as the SaveToFile method.
However, SaveToStream writes to the current position in the target stream.
Thus, for example, SaveToStream can be used to append the contents of Memory
to a file stream, rather than replace the contents of the file the way
SaveToFile do
es.

If the entire contents of the memory stream cannot be written to the target stream, SaveToStream raises an EWriteError exception.
 
procedure TForm1.Button1Click(Sender: TObject);
var
l_MemoryStream : TMemoryStream;
l_FileStream : TFileStream;
begin
l_MemoryStream := TMemoryStream.create;
l_MemoryStream.loadfromfile('d:/bb.txt');
l_FileStream := TFileStream.Create('d:/aa.txt', fmOpenWrite + fmShareDenyNone);
l_FileStream.size:=0;
//////加这行,你原来的应该还是写在头上,但如果原文件长,后面的内容还在。
l_MemoryStream.SaveToStream(l_FileStream);
end;
 
Tfilestream 好象不能控制长度,重建 aaa 文件
l_MemoryStream := TMemoryStream.create;
l_FileStream := TFileStream.Create('c:/tmp/aaa', fmOpenWrite + fmShareDenyNone);
l_FileStream.seek(0, soFrombegin
ning);
l_FileStream.free;
l_FileStream.TFileStream.Create('c:/tmp/aaa', fmCreate);
l_MemoryStream.SaveToStream(l_FileStream);
 
真是奇怪,我使用savetostream就是增加文件的内容,而不是覆盖,
有什么办法???
 
没有问题的,我这边都试过了。你先用文本文件试试。
 

Similar threads

I
回复
0
查看
2K
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
618
import
I
I
回复
0
查看
843
import
I
后退
顶部