Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reading a string from TMemoryStream

134 views
Skip to first unread message

Roberto Meneghini

unread,
Jul 20, 2006, 12:59:26 PM7/20/06
to
Hello,

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


Craig Stuntz [TeamB]

unread,
Jul 20, 2006, 1:32:10 PM7/20/06
to
Roberto Meneghini wrote:

> 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

Roberto Meneghini

unread,
Jul 20, 2006, 5:43:04 PM7/20/06
to
Hello Graig,

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]

unread,
Jul 21, 2006, 7:58:39 AM7/21/06
to
You could pull the characters one at a time into a StringBuilder.

--
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

Ray Konopka

unread,
Nov 30, 2006, 2:54:49 AM11/30/06
to
Hi Craig,

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

Ray Konopka

unread,
Nov 30, 2006, 3:52:06 PM11/30/06
to
Hi Roberto,

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

0 new messages