Lazarus
Home
Help
TinyPortal
Search
Login
Register
Lazarus
»
Forum
»
Programming
»
General
»
[SOLVED] TMemoryStream.ReadAnsiString Error
Free Pascal
Website
Downloads
Wiki
Documentation
Bugtracker
Mailing List
Lazarus
Website
Downloads (Laz+FPC)
Packages (OPM)
FAQ
Wiki
Documentation (RTL/FCL/LCL)
Bugtracker
CCR Bugs
IRC channel
GIT
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
How to use the forum
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
Controlling the behavior ...
by
rvk
[
Today
at 08:31:48 am]
A "leisure" question on W...
by
Awkward
[
Today
at 07:59:12 am]
Curling Pythagoras Tree: ...
by
AlexTP
[
Today
at 07:45:46 am]
Free AI to use in local
by
Thaddy
[
Today
at 07:36:24 am]
Big Numbers Math
by
Thaddy
[
Today
at 07:22:49 am]
how to change the color f...
by
Thaddy
[
Today
at 07:14:33 am]
Parquet?
by
Thaddy
[
Today
at 07:11:17 am]
STARS and Regular POLYGON...
by
Boleeman
[
Today
at 06:41:41 am]
Offical launch of the 1 B...
by
Gustavo 'Gus' Carreno
[
Today
at 05:53:09 am]
Project fails on run but ...
by
jamie
[
Today
at 12:54:49 am]
Ribbon bar in Lazarus 3.2...
by
wp
[March 28, 2024, 11:29:17 pm]
how to search in rxmemory...
by
rcmz
[March 28, 2024, 10:19:54 pm]
Configure JCF messages
by
GordonFindlay
[March 28, 2024, 10:16:19 pm]
Poll: Watches and Display...
by
Martin_fr
[March 28, 2024, 10:15:08 pm]
Dialog ShowModal and an i...
by
d7_2_laz
[March 28, 2024, 09:51:39 pm]
JEDI error but unit compi...
by
Thaddy
[March 28, 2024, 09:30:33 pm]
Understanding and adjusti...
by
circular
[March 28, 2024, 09:09:56 pm]
Dynarray initialization p...
by
Thaddy
[March 28, 2024, 06:30:12 pm]
Zeos components. Had to c...
by
Fantablup
[March 28, 2024, 04:17:13 pm]
[SOLVED] Completion box s...
by
furious programming
[March 28, 2024, 04:12:26 pm]
[LAMW] How to create a ca...
by
af0815
[March 28, 2024, 03:48:16 pm]
Complex package installat...
by
wp
[March 28, 2024, 03:38:16 pm]
[SOLVED] Combined pointer...
by
CM630
[March 28, 2024, 02:48:41 pm]
Using KeyPress event, how...
by
Zvoni
[March 28, 2024, 02:24:42 pm]
a Unit problem?
by
Thaddy
[March 28, 2024, 01:56:12 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: [SOLVED] TMemoryStream.ReadAnsiString Error (Read 1227 times)
Handoko
Hero Member
Posts: 5131
My goal: build my own game engine using Lazarus
[SOLVED] TMemoryStream.ReadAnsiString Error
«
on:
July 14, 2020, 08:11:45 pm »
Did I do it wrong?
Why the first attempt to convert TMemoryStream's data to string is okay but on the second attempt I got an
EReadError
exception '
Stream read error
' message.
Code: Pascal
[Select]
[+]
[-]
unit
Unit1
;
{$mode objfpc}{$H+}
interface
uses
Classes
,
SysUtils
,
Forms
,
Controls
,
Dialogs
,
StdCtrls
;
type
{ TForm1 }
TForm1
=
class
(
TForm
)
Button1
:
TButton
;
procedure
Button1Click
(
Sender
:
TObject
)
;
end
;
var
Form1
:
TForm1
;
implementation
{$R *.lfm}
{ TForm1 }
procedure
TForm1
.
Button1Click
(
Sender
:
TObject
)
;
const
Data
=
'Testing'
;
var
aMemoryStream
:
TMemoryStream
;
aString
:
string
;
i
:
Integer
;
begin
aMemoryStream
:
=
TMemoryStream
.
Create
;
for
i
:
=
1
to
Data
.
Length
do
aMemoryStream
.
WriteByte
(
Ord
(
Data
[
i
]
)
)
;
// First read attempt - OK
aString
:
=
''
;
aMemoryStream
.
Position
:
=
0
;
for
i
:
=
1
to
aMemoryStream
.
Size
do
aString
:
=
aString
+
Chr
(
aMemoryStream
.
ReadByte
)
;
ShowMessage
(
aString
)
;
// Second read attempt - FAILED
aMemoryStream
.
Position
:
=
0
;
aString
:
=
aMemoryStream
.
ReadAnsiString
;
ShowMessage
(
aString
)
;
aMemoryStream
.
Free
end
;
end
.
Tested on Lazarus 2.0.10 Linux 64-bit.
«
Last Edit: July 14, 2020, 08:33:03 pm by Handoko
»
Logged
cai
New Member
Posts: 41
Re: TMemoryStream.ReadAnsiString Error
«
Reply #1 on:
July 14, 2020, 08:22:47 pm »
I think to call ReadAnsiString, you need to call WriteAnsiString First.
if you look in ReadAnsiString source code, you will see it need to read size first, there is no correct size in you stream cause you just WriteByte
Logged
howardpc
Hero Member
Posts: 4144
Re: TMemoryStream.ReadAnsiString Error
«
Reply #2 on:
July 14, 2020, 08:26:31 pm »
Try this:
Code: Pascal
[Select]
[+]
[-]
procedure
TForm1
.
Button1Click
(
Sender
:
TObject
)
;
const
Data
=
'Testing'
;
var
aMemoryStream
:
TMemoryStream
;
aString
:
string
;
i
:
Integer
;
begin
aMemoryStream
:
=
TMemoryStream
.
Create
;
for
i
:
=
1
to
Data
.
Length
do
aMemoryStream
.
WriteByte
(
Ord
(
Data
[
i
]
)
)
;
aString
:
=
''
;
aMemoryStream
.
Position
:
=
0
;
for
i
:
=
1
to
aMemoryStream
.
Size
do
aString
:
=
aString
+
Chr
(
aMemoryStream
.
ReadByte
)
;
ShowMessage
(
aString
)
;
aMemoryStream
.
Clear
;
aMemoryStream
.
WriteAnsiString
(
Data
)
;
aMemoryStream
.
Position
:
=
0
;
aString
:
=
aMemoryStream
.
ReadAnsiString
;
ShowMessage
(
aString
)
;
aMemoryStream
.
Free
end
;
Logged
Handoko
Hero Member
Posts: 5131
My goal: build my own game engine using Lazarus
Re: TMemoryStream.ReadAnsiString Error
«
Reply #3 on:
July 14, 2020, 08:32:42 pm »
cai, you're right. And thanks howardpc. Now, I have better understanding about streams.
I was writing code to read data from the web and I need to do some processings before showing it on the screen. I know how to do it now.
Logged
TRon
Hero Member
Posts: 2435
Re: [SOLVED] TMemoryStream.ReadAnsiString Error
«
Reply #4 on:
July 14, 2020, 08:49:01 pm »
@Handoko:
Alternatively, and probably depending on your needs/requirements, you could perhaps also make use of TStringStream
https://www.freepascal.org/docs-html/rtl/classes/tstringstream.html
It is a descendant of TMemoryStream so you would also still be able to manipulate the memory.
Logged
Handoko
Hero Member
Posts: 5131
My goal: build my own game engine using Lazarus
Re: [SOLVED] TMemoryStream.ReadAnsiString Error
«
Reply #5 on:
July 15, 2020, 04:51:54 am »
Yes, you're right. TStringStream has DataString property, which make it easier to use.
Thank you for the suggestion.
Logged
PascalDragon
Hero Member
Posts: 5446
Compiler Developer
Re: TMemoryStream.ReadAnsiString Error
«
Reply #6 on:
July 15, 2020, 09:47:50 am »
Quote from: cai on July 14, 2020, 08:22:47 pm
I think to call ReadAnsiString, you need to call WriteAnsiString First.
Correct.
ReadAnsiString
expects a specific format inside the stream, namely the length followed by the string data and that is created by
WriteAnsiString
. To read plain string data you need to use the other
Read
methods and manually preallocate enough space.
Logged
rvk
Hero Member
Posts: 6112
Re: [SOLVED] TMemoryStream.ReadAnsiString Error
«
Reply #7 on:
July 15, 2020, 02:56:27 pm »
Yes, if you have a choice, TStringStream is easiest.
If you don't have a choice you can also access TMemoryStream.Memory directly.
Code: Pascal
[Select]
[+]
[-]
function
MemoryStreamToString
(
M
:
TMemoryStream
)
:
string
;
begin
SetString
(
Result
,
PChar
(
M
.
Memory
)
,
M
.
Size
div
SizeOf
(
Char
)
)
;
end
;
Logged
Print
Pages: [
1
]
« previous
next »
Lazarus
»
Forum
»
Programming
»
General
»
[SOLVED] TMemoryStream.ReadAnsiString Error
TinyPortal
© 2005-2018