Anybody have a good method of clearing a TStringStream?
John McTaggart
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
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)
>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
What would either do that Size := 0; wouldn't?
--
Regards
Ralph (TeamB)
--
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).
>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"
>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
>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
>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.
The two lines I gave you were not meant to be alternatives, but a two
line solution.
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.
>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
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