I'm trying to read a string from a TMemoryStream but it always comes back
with just the first character. This is how I'm doing it:
var
Source, Target : Strings;
Size: Integer;
Buffer: array of char;
ms: TMemoryStream;
:
Size := Source.Length;
ms.Write(Size);
ms.WriteBuffer(Source[1], Size); // I've tried with Write
ms.Position := 0;
ms.Read(Size); // I've checked the size read and it matches the length of
the source string
SetLength(Buffer, Size);
ms.ReadBuffer(Buffer[0], Size); // only the first character is read, the
rest is left as zero
Target := String(Buffer);
I'm using Delphi VCL.NET that ships with BDS2006 SP2.
Thanks,
Roberto
> ms.ReadBuffer(Buffer[0], Size); // only the first character is read,
> the rest is left as zero Target := String(Buffer);
>
> I'm using Delphi VCL.NET that ships with BDS2006 SP2.
String indexing in .NET creates a new string, IIRC. Is there a reason
you're not using TStringStream?
--
Craig Stuntz [TeamB] · Vertex Systems Corp. · Columbus, OH
Delphi/InterBase Weblog : http://blogs.teamb.com/craigstuntz
Everything You Need to Know About InterBase Character Sets:
http://blogs.teamb.com/craigstuntz/articles/403.aspx
I'm writing and reading other type of variables (Integer, double, etc.)
using the same stream. Basically, I have a class that exposes a
WriteToStream and LoadFromStream methods. It uses the generic TStream
because sometimes it needs to deal with TFileStream and other times with
TMemoryStream.
Regards,
Roberto
"Craig Stuntz [TeamB]" <craig_...@nospam.please [a.k.a. acm.org]> wrote
in message news:44bfbe1a$1...@newsgroups.borland.com...
--
Craig Stuntz [TeamB] · Vertex Systems Corp. · Columbus, OH
Delphi/InterBase Weblog : http://blogs.teamb.com/craigstuntz
Want to help make Delphi and InterBase better? Use QC!
http://qc.borland.com -- Vote for important issues
The issue is not with the string. The problem is that the call
to ReadBuffer is not populating the other entries in the buffer.
That is, only the first array element of the buffer gets populated.
The other elements are set to 0, even though the count
returned from ReadBuffer is accurate.
The same problem happens with the standard TStream.Read
method in VCL.NET. Only the first element of the buffer
gets populated. The remaining elements are 0 even though
there is indeed data.
Ray
In case anyone is interested. The reason the following fails to populate
the entire Buffer is because of the specification of the offset [0] in the
call to ReadBuffer. This is necessary in VCL, but in VCL.NET this
cause the reported problem (i.e. only the first item in the buffer is
populated).
The solution to the problem is to remove the offset from the ReadBuffer
call.
ms.ReadBuffer( Buffer, Size );
The buffer will now be correctly populated.
-Ray