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?
Related Topics
PHP: Check Strings with Ctype-Functions for Character Classes
Article | 0 Comments
Delphi/Lazarus: Store and Save MemoryStream in FileStream
Info | 0 Comments
PHP: Remove arbitrary Characters at the Beginning and the End of a String
Tutorial | 0 Comments
Textarea Maxlength: Limit Maximum Number of Characters in Textarea
Tutorial | 3 Comments
How to fix "The File or Directory is corrupted and unreadable" (Error 0x80070570)
Question | 1 Answer
Rewrite Text Files with a fixed Line Length
Tutorial | 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.
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:
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