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

Clearing a stringstream

875 views
Skip to first unread message

John McTaggart

unread,
Jul 30, 1999, 3:00:00 AM7/30/99
to
Hi,

Anybody have a good method of clearing a TStringStream?

John McTaggart

John McTaggart

unread,
Jul 30, 1999, 3:00:00 AM7/30/99
to
Hi,

I forgot to mention that the method I'm currently using to clear the
stringstream is...

{ Clears stream (sort of) actually it sets it to 1 byte in length }

Nada := ' ';
FStrBuffer.Seek(0, soFromBeginning);
FStrBuffer.WriteBuffer(Pointer(Nada)^, Length(Nada));

John McTaggart


Mike Orriss (TeamB)

unread,
Jul 30, 1999, 3:00:00 AM7/30/99
to
In article <37a1b40d...@forums.borland.com>, John McTaggart wrote:
> Anybody have a good method of clearing a TStringStream?
>

How about:

StringStream.Position := 0;
StringStream.WriteString('');

Mike Orriss (TeamB)
(Unless stated otherwise, my replies relate to Delphi 4.03)
(Unsolicited e-mail replies will most likely be ignored)


John McTaggart

unread,
Jul 31, 1999, 3:00:00 AM7/31/99
to
On Fri, 30 Jul 1999 19:15:01 +0100, "Mike Orriss (TeamB)"
<m...@3kcc.co.uk> wrote:

>In article <37a1b40d...@forums.borland.com>, John McTaggart wrote:
>> Anybody have a good method of clearing a TStringStream?
>>
>
>How about:
>
> StringStream.Position := 0;

This one works fine, but simply moves the pointer to the beginning of
the stream. It doesn't make it 0 bytes in length.

> StringStream.WriteString('');

This was what I tried on my first attempt and it caused an exception.

The correct answer was sent to me via e-mail from David Baer who made
the suggestion to use...

StringStream.Size := 0;

Which make perfect sense, but for some unknown reason, I had a mental
block that kept telling me that Size was readonly (maybe because the
help file shows it as readonly <g>), when in fact it allows you to set
it, so it must really be read/write.

Just a nit to pick, but why Borland chose not to include Clear and
DeleteString methods is beyond me. Passing a string in the constructor
is kinda goofy as well...

TStringStream is an extraordinarily handy class, but these omissions
leave it a little lacking.

Oh well...

BTW, was it you that submitted the Nodes example to Code Central for
populating a TreeView from a stringlist?

Thanks

John McTaggart

Ralph Friedman (TeamB)

unread,
Jul 31, 1999, 3:00:00 AM7/31/99
to
In article <37a25da1...@forums.borland.com>, John McTaggart
stated:

> Just a nit to pick, but why Borland chose not to include Clear and
> DeleteString methods is beyond me. Passing a string in the constructor
> is kinda goofy as well...
>
John,

What would either do that Size := 0; wouldn't?
--
Regards
Ralph (TeamB)
--


Mike Orriss (TeamB)

unread,
Jul 31, 1999, 3:00:00 AM7/31/99
to
In article <37a25da1...@forums.borland.com>, John McTaggart
wrote:

> >How about:
> > StringStream.Position := 0;

> This one works fine, but simply moves the pointer to the beginning of
> the stream. It doesn't make it 0 bytes in length.

That was all it was intended to do <g>


> > StringStream.WriteString('');

> This was what I tried on my first attempt and it caused an exception.

Worked fine for me - I did test it before replying to you. I have to
admit though that Size:=0 is a better solution (but it didn't occur to
me).

Ray Lischner

unread,
Jul 31, 1999, 3:00:00 AM7/31/99
to
On Sat, 31 Jul 1999 02:24:53 GMT, t...@ria.net (John McTaggart) wrote:

>Passing a string in the constructor
>is kinda goofy as well...

It makes sense if you want to read from an existing string.

--
Ray Lischner (http://www.tempest-sw.com/)
Author of "Hidden Paths of Delphi 3: Experts, Wizards, and the Open Tools API"

John McTaggart

unread,
Aug 1, 1999, 3:00:00 AM8/1/99
to
On Sat, 31 Jul 1999 06:17:55 +0200, "Ralph Friedman (TeamB)"
<ralphfriedman@spamno_email.com> wrote:

>In article <37a25da1...@forums.borland.com>, John McTaggart

>stated:
>> Just a nit to pick, but why Borland chose not to include Clear and

>> DeleteString methods is beyond me. Passing a string in the constructor


>> is kinda goofy as well...
>>

>John,
>
> What would either do that Size := 0; wouldn't?

Size := 0 would be fine for Clear, but it wouldn't do a thing for
DeleteString. To my way of thinking, DeleteString should allow you to
remove a single chunk of the stringstream, not the entire
stringstream. Something like...

DeleteString(Position, Count: LongInt);

Which can be done fairly easily, but it's simply not there. I guess
I'm just trying to figure out their reasoning for having a Clear
method in most of the other Stream based objects while this one goes
without...

John McTaggart


John McTaggart

unread,
Aug 1, 1999, 3:00:00 AM8/1/99
to
On Sat, 31 Jul 1999 08:51:01 +0100, "Mike Orriss (TeamB)"
<m...@3kcc.co.uk> wrote:

>In article <37a25da1...@forums.borland.com>, John McTaggart

>wrote:
>> >How about:
>> > StringStream.Position := 0;
>
>> This one works fine, but simply moves the pointer to the beginning of
>> the stream. It doesn't make it 0 bytes in length.
>
>That was all it was intended to do <g>

But that's not the result that I needed! <g>

>> > StringStream.WriteString('');
>
>> This was what I tried on my first attempt and it caused an exception.
>
>Worked fine for me - I did test it before replying to you.

Actually, I think this was a case of me not explaining what I was
doing. WriteString can handle a blank string (''), but it merely
appends it to the stream. The constructor on the other hand can't deal
with it at all...

StringStream := TStringStream.Create('');

..throws an exception.

> I have to admit though that Size:=0 is a better solution (but it didn't occur to
> me).

It didn't occur to me either since the help file said it was read
only...

John McTaggart

Ray Lischner

unread,
Aug 2, 1999, 3:00:00 AM8/2/99
to
On Sun, 01 Aug 1999 22:59:28 GMT, t...@ria.net (John McTaggart) wrote:

>To my way of thinking, DeleteString should allow you to
>remove a single chunk of the stringstream, not the entire
>stringstream. Something like...
>
> DeleteString(Position, Count: LongInt);

In that case, you don't have a stream. A stream, as its name implies,
is a sequence of something, usually characters or bytes. You can read
from a stream, or write to a stream. You cannot insert or delete. If
you need that functionality, you don't have a stream.

TMemoryStream is the only stream class to implement Clear, and that is
because of its internal representation. Clear is subtly different from
setting Size := 0.

Mike Orriss (TeamB)

unread,
Aug 2, 1999, 3:00:00 AM8/2/99
to
In article <37a4d100...@forums.borland.com>, John McTaggart
wrote:

> But that's not the result that I needed! <g>
>

The two lines I gave you were not meant to be alternatives, but a two
line solution.

lj

unread,
Aug 3, 1999, 3:00:00 AM8/3/99
to
John McTaggart said something like...

|
|StringStream := TStringStream.Create('');
|
|..throws an exception.
|

StringStream.Create('') is being used in an application I run every day.
I am using D3.02. If D4 is throwing an exception, I would compare the
source code to find out where the bug is.

John McTaggart

unread,
Aug 4, 1999, 3:00:00 AM8/4/99
to
On Mon, 02 Aug 1999 00:20:22 GMT, delphi.at.te...@nospam.com
(Ray Lischner) wrote:

>On Sun, 01 Aug 1999 22:59:28 GMT, t...@ria.net (John McTaggart) wrote:
>
>>To my way of thinking, DeleteString should allow you to
>>remove a single chunk of the stringstream, not the entire
>>stringstream. Something like...
>>
>> DeleteString(Position, Count: LongInt);
>
>In that case, you don't have a stream. A stream, as its name implies,
>is a sequence of something, usually characters or bytes.

So is a string. The biggest difference I see is the mechanism used to
get to the information contained in each.

>TMemoryStream is the only stream class to implement Clear, and that is
>because of its internal representation. Clear is subtly different from
>setting Size := 0.

The only real difference that I see is that TMemoryStream uses
SetCapacity(0) and positions the pointer to the beginning of the
stream in its Clear method.

John McTaggart

John McTaggart

unread,
Aug 4, 1999, 3:00:00 AM8/4/99
to
On Tue, 3 Aug 1999 14:01:56 -0400, lind...@bellatlantic.net (lj)
wrote:

I'm using 3.02 as well.

I can't explain it, but after trying it again, it worked. I have no
idea why...

John McTaggart

0 new messages