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
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
I can write your README.m...
by
GAN
[
Today
at 04:09:39 am]
Wiki access blocked
by
dsiders
[
Today
at 03:39:46 am]
Why does this happen
by
dbannon
[
Today
at 01:47:37 am]
Raspberry 5 Bookworm Laza...
by
dbannon
[
Today
at 01:43:04 am]
Console User Interfaces
by
Curt Carpenter
[
Today
at 01:05:51 am]
TShape.SetTempCursor, how...
by
jamie
[
Today
at 12:18:03 am]
Codetools bug
by
440bx
[May 19, 2025, 11:39:57 pm]
Tone Generator
by
Gigatron
[May 19, 2025, 10:41:46 pm]
Icon file was loaded on m...
by
AlexTP
[May 19, 2025, 10:40:45 pm]
crtbeginS.o and crtendS.o...
by
munair
[May 19, 2025, 10:18:46 pm]
Error trying to initializ...
by
Fibonacci
[May 19, 2025, 10:13:29 pm]
Inconsistent function des...
by
440bx
[May 19, 2025, 09:26:07 pm]
Acessing a file dialog fr...
by
tfurnivall
[May 19, 2025, 09:23:44 pm]
Learn how to (un)dock a T...
by
Hansvb
[May 19, 2025, 07:42:28 pm]
Recortar (trim) archivo d...
by
FelizKatus
[May 19, 2025, 05:57:45 pm]
IDE locks the work if pro...
by
AlexTP
[May 19, 2025, 04:25:33 pm]
Rotating 3D Supershape (S...
by
Boleeman
[May 19, 2025, 04:13:04 pm]
BGRA Controls
by
mizar
[May 19, 2025, 03:35:45 pm]
ProjectTemplates - Copy s...
by
paweld
[May 19, 2025, 02:41:28 pm]
Lazbarcode 3of9 - half ba...
by
Int3g3r
[May 19, 2025, 02:23:41 pm]
Periodic Table
by
VisualLab
[May 19, 2025, 01:38:19 pm]
Trim audio file
by
FelizKatus
[May 19, 2025, 01:25:36 pm]
InstallAware 2025 Sources...
by
msintle
[May 19, 2025, 01:01:28 pm]
Negative offset
by
LemonParty
[May 19, 2025, 12:58:11 pm]
[SOLVED] How to "Jump To"...
by
dbannon
[May 19, 2025, 12:55:28 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: [Solved] SaveToFile with TMemoryStream (Read 1254 times)
jcmontherock
Sr. Member
Posts: 294
[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.0-64 - FPC 3.2.2
Fibonacci
Hero Member
Posts: 717
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: 294
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.0-64 - FPC 3.2.2
Fibonacci
Hero Member
Posts: 717
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