急!急!如何在流Stream中存取字符串(50分)

  • 主题发起人 主题发起人 agang
  • 开始时间 开始时间
A

agang

Unregistered / Unconfirmed
GUEST, unregistred user!
MyStream:= TFileStream.Create('xxx.xxx', fmCreate);
try
PChar:= PChar(AString);
Size:= Length(PChar);
MyStream.Write(Size, SizeOf(Integer));
MyStream.Write(PChar, Size);

PChar:= PChar(BString);
Size:= Length(PChar);
MyStream.Write(Size, SizeOf(Integer));
MyStream.Write(PChar, Size);
finnaly
MyStream.Free;
end;

...........
...........
...........
用相反的方法读取AString, BString
MyStream:= TFileStream.Create('xxx.xxx', fmOpenRead);
try
MyStream.Read(Size, SizeOf(Integer));
MyStream.Read(PChar, Size);
AString:= PChar;

MyStream.Read(Size, SizeOf(Integer));
MyStream.Read(PChar, Size);
BSTring:=PChar;
finnaly
MyStream.Free;
end;

为什么?上天如此爱捉弄人,此方法时好时坏
但读出乱码的时候更多
那位高手愿意出手救救苦命的人呐
 
try
MyStream.Read(Size, SizeOf(Integer));
--->> GetMem(PChar, Size)
// Add This.
MyStream.Read(PChar, Size);
 
发一篇文章给你,好好看看.
 
请看

MyStream:= TFileStream.Create('xxx.xxx', fmCreate);
try
MyStream.Write(astring[1], length(astring))

//或者可以用 MyStream.Write(pchar(astring)^, length(astring));
MyStream.Write(PChar, Size);
finnaly
MyStream.Free;
end;

 
可以试一试用 TReader 读,用 TWriter 写。
...
var
Stream: TFileStream;
Reader: TReader;
begin
Stream := TFileStream.Create(FileName, fmOpenRead);
try
LoadFromStream(Stream);
Reader:=TReader.Create(Stream, $FF);
try
with Reader do
begin
ReadListBegin;
S := Reader.ReadString;
...
ReadListEnd;
end;
finally
Reader.Free;
end;
finally
Stream.Free;
end;
end;
 
Another_eYes是对的

MyStream.Read(Size, SizeOf(Integer));
--->> GetMem(PChar, Size)
// Add This.
ReallocMem(PChar, Size)
//最好.
MyStream.Read(PChar, Size);


Another_eYes 认为呢?
 
多人接受答案了。
 
后退
顶部