Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Proper way to convert a stream to and from a string?

Status
Not open for further replies.

djjd47130

Programmer
Nov 1, 2010
480
US
I've had a few cases when I had to read/write a stream (specifically TMemoryStream) and sync it with a string. However, every time I've tried, I failed. The little sample code I've found is also not telling the whole story and such. One main problem is when I need to clear the data in the stream and re-use it for something else. Sometimes I have strange exceptions when trying to free a memory stream.

What's the best and most recommended method for translating a stream to and from a string? In the end it will consist of using TBitmap.SaveToStream(MyStream) and TJDClientSocket.Socket.SendStream(MyStream)...

Each of these streams may be re-used without freeing, but that's where I seem to have the problems, when I clear the data and add new data. I know my current project will require me to do this, so I need to get it down.

I'll probably wind up wrapping it into some class like TStringStream and provide string routines to read/write with the stream.


JD Solutions
 
Use TStringStream. You can copy a TMemoryStream to and from a TStringStream, and access the DataString property to read the contents. Remember to set the Position of each stream to 0 prior to using SaveToStream.
 
Don't tell me TStringStream already exists :p How may streams are there?

JD Solutions
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top