00 Votes

Delphi: MemoryStream contains/stores only unreadable characters and hieroglyphs

Question by Chematik | 2013-07-27 at 20:29

In Delphi, I have a TMemoryStream and I would like to read and save this stream. But, no matter what I am doing, for some reason, the stream only contains unreadable characters. So that you can better imagine the problem, here is a little extract of the results:

°°  €g ½       0ì           ð­º  º ­     º  
º ­º
ð ­º‘ À 7 „< 4 >º ð ­º ­º º ­º º ­º ð­ºð­ºð­º ð­ º ‘ Àš F ôD ð­ºð­
ð­ º‘ À‘ „E d D €g ¥J 0 d

The interesting with this is: when saving this fragmented MemoryStream as file and when determining the size, this size exactly is equalent to the size of the data, that should normally be stored in the stream. The only problem is, that it is the wrong data. But how can this happen?

ReplyPositiveNegative
Avatar
1Best Answer1 Vote

This sounds like you do not have set the position of the MemoryStream back to 0 before reading. Please insert the following line before the processing of the stream:

// ms: TMemoryStream;

ms.position:=0;

Explaination:

When storing something in a stream, Delphi recognizes the current position using a pointer. After writing, this position usually stands at the end of the stream, so that you can go on writing at this position.

Of course, in the case, you would like to process or read the stream, you have to go back to the beginning of the stream. If you do not do this, Delphi starts reading at the end of the stream and so, the program reads the waste stored in the memory after computer located physicality after the place where your stream is located in the memory (this waste are the hieroglyphs you can see). When using ms.Size, Delphi reads the waste in a length that is equal to the length of your original stream - this is why the length is right like you have written.

Example:

Regarding this topic, I have written the information Load and Store MemoryStream in FileStream. In this text, you can see an example of how you can solve the problem. In this example, you can also see the line where the position is set to 0.
2013-07-30 at 17:14

ReplyPositive Negative
Reply

Related Topics

Delphi: Password Edit

Tip | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.