Lazarus
Home
Help
TinyPortal
Search
Login
Register
Lazarus
»
Forum
»
Free Pascal
»
General
(Moderators:
FPK
,
Tomas Hajny
) »
[Solved] SaveToFile with TMemoryStream
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
GIT
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
How to use the forum
Forum Rules
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
Lazarus broke my project
by
af0815
[
Today
at 10:59:53 am]
SandTris (Tetris variant)
by
Boleeman
[
Today
at 10:43:15 am]
Fpcupdeluxe
by
DonAlfredo
[
Today
at 10:39:15 am]
Lowercase Drive in LAZ 4....
by
JuhaManninen
[
Today
at 10:18:09 am]
New Mac mini M4 - FPC 3.2...
by
mischi
[
Today
at 09:32:46 am]
Get "serial number" of (r...
by
Thaddy
[
Today
at 09:25:53 am]
Some testing the water qu...
by
paweld
[
Today
at 07:07:33 am]
WaveFunction Collapse
by
Boleeman
[
Today
at 04:11:50 am]
Free Vision Build Failing...
by
swissbob
[November 15, 2025, 11:54:34 pm]
Add new unit to a Lazarus...
by
n7800
[November 15, 2025, 11:50:01 pm]
Programmatic filtering in...
by
jamie
[November 15, 2025, 08:05:46 pm]
How to hide IEEE754 limit...
by
srvaldez
[November 15, 2025, 07:42:38 pm]
This worries me about our...
by
Curt Carpenter
[November 15, 2025, 06:32:55 pm]
Porting PDF Viewer from D...
by
Boleeman
[November 15, 2025, 06:21:52 pm]
Form scaling aspect ratio
by
jamie
[November 15, 2025, 05:01:41 pm]
Demo #3 of my "home grown...
by
TBMan
[November 15, 2025, 03:05:57 pm]
Seems like good timing to...
by
mdlueck
[November 15, 2025, 02:17:23 pm]
Lazarus Bugfix Release 4...
by
jwdietrich
[November 15, 2025, 01:54:34 pm]
synedit
by
Martin_fr
[November 15, 2025, 01:49:12 pm]
[SOLVED] How to update 'F...
by
Hartmut
[November 15, 2025, 01:33:36 pm]
Help with search and repa...
by
avk
[November 15, 2025, 07:09:59 am]
Variable "xyz" doesn't no...
by
Mr.Madguy
[November 15, 2025, 06:22:15 am]
Problem Installing CHM He...
by
Curt Carpenter
[November 15, 2025, 03:03:11 am]
EditorMacroScript did not...
by
jamie
[November 15, 2025, 02:55:41 am]
Mapping Images Like Odome...
by
SandyG
[November 14, 2025, 11:53:01 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: [Solved] SaveToFile with TMemoryStream (Read 1428 times)
jcmontherock
Sr. Member
Posts: 316
[Solved] SaveToFile with TMemoryStream
«
on:
June 15, 2024, 05:59:30 pm »
I wanted to export a MySQL table to SQL. This table contains about 500.000 rows. I tried avoiding problems with that number of rows. I created an AnsiString time to time each 2000 rows. It works fine except that at each beginning of string I get in the file some binary characters. The code is very simple:
Code: Pascal
[Select]
[+]
[-]
// aStr is defined as AnsiString
procedure
TForm1
.
FormShow
(
Sender
:
TObject
)
;
begin
MStr
:
=
TMemoryStream
.
Create
;
MStr
.
Position
:
=
0
;
aStr
:
=
'-- '
+
nl
;
aStr
+=
'SET SQL_MODE='
'NO_AUTO_VALUE_ON_ZERO'
';'
+
nl
;;
aStr
+=
'-- SET AUTOCOMMIT = 0;'
+
nl
;
aStr
+=
'-- START TRANSACTION;'
+
nl
;;
aStr
+=
'-- SET time_zone = "+00:00";'
+
nl
;
aStr
+=
''
;
aStr
+=
'-- ---------------------------------------------------'
+
nl
;
aStr
+=
'-- '
+
nl
;
aStr
+=
'-- File encoding is UTF8 '
+
nl
;
aStr
+=
'set character set utf8;'
+
nl
;
aStr
+=
'-- ---------------------------------------------------'
+
nl
;
aStr
+=
'-- '
+
nl
;
MStr
.
WriteAnsiString
(
aStr
)
;
MStr
.
Position
:
=
0
;
MStr
.
SaveToFile
(
'aStrFile.sql'
)
;
MStr
.
Free
;
the file begins with 4 binary bytes. In that ex: 29010000. How to avoid such insertion at each WriteAnsiString ?
«
Last Edit: June 16, 2024, 11:03:52 am by jcmontherock
»
Logged
Windows 11 UTF8-64 - Lazarus 4.2-64 - FPC 3.2.2
Fibonacci
Hero Member
Posts: 788
Internal Error Hunter
Re: SaveToFile with TMemoryStream
«
Reply #1 on:
June 15, 2024, 06:10:13 pm »
WriteAnsiString
writes the length of the string at the beginning, use
Write
or
WriteBuffer
Logged
jcmontherock
Sr. Member
Posts: 316
Re: SaveToFile with TMemoryStream
«
Reply #2 on:
June 15, 2024, 06:20:41 pm »
Thanks a lot. I changed: "MStr.WriteAnsiString(aStr);" to "MStr.WriteBuffer(aStr[1], Length(aStr));" and it works. In which situations do you use "WriteAnsiString" ?
«
Last Edit: June 15, 2024, 06:23:14 pm by jcmontherock
»
Logged
Windows 11 UTF8-64 - Lazarus 4.2-64 - FPC 3.2.2
Fibonacci
Hero Member
Posts: 788
Internal Error Hunter
Re: SaveToFile with TMemoryStream
«
Reply #3 on:
June 15, 2024, 06:28:10 pm »
Quote from: jcmontherock on June 15, 2024, 06:20:41 pm
In which situations do you use "WriteAnsiString" ?
For example, if you want to save few strings to be read later, like a config file, so later on each call to ReadAnsiString you get next string
Logged
Print
Pages: [
1
]
« previous
next »
Lazarus
»
Forum
»
Free Pascal
»
General
(Moderators:
FPK
,
Tomas Hajny
) »
[Solved] SaveToFile with TMemoryStream
TinyPortal
© 2005-2018