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
Read/Parse PDB file to ge...
by
tooknox
[
Today
at 07:05:09 pm]
StringGrid: which is "cur...
by
Bart
[
Today
at 06:57:50 pm]
Pure Pascal LZ4, LZ5 and ...
by
LemonParty
[
Today
at 06:50:43 pm]
Scripts to generate ofici...
by
robert rozee
[
Today
at 05:38:51 pm]
Pls support Double Comman...
by
Hansvb
[
Today
at 03:30:31 pm]
Can I get the position an...
by
CM630
[
Today
at 02:37:08 pm]
When is it useful for a p...
by
Hansvb
[
Today
at 02:18:50 pm]
text color visible
by
Paolo
[
Today
at 12:10:14 pm]
Transparent form
by
rvk
[
Today
at 11:02:39 am]
Gtk3 widgetset - call for...
by
zeljko
[
Today
at 10:23:59 am]
Connecting to AzureSQL
by
CharlyTango
[
Today
at 10:21:29 am]
Qt does not position form...
by
dbannon
[
Today
at 09:42:44 am]
Artificial Intelligence a...
by
Thaddy
[
Today
at 06:06:51 am]
FPC Unleashed (inline var...
by
Fibonacci
[
Today
at 06:06:41 am]
show me the parameters ce...
by
n7800
[May 13, 2026, 11:14:34 pm]
How to compare floating p...
by
Sander
[May 13, 2026, 11:12:02 pm]
TA Chart Axes visibility ...
by
Nicole
[May 13, 2026, 08:27:54 pm]
new feature for IDE
by
n7800
[May 13, 2026, 08:20:14 pm]
Can /my/ AI help me with ...
by
440bx
[May 13, 2026, 07:28:37 pm]
SynEdit auto End of line ...
by
eldonfsr
[May 13, 2026, 05:02:39 pm]
[beyond fixing]FPGMAP acc...
by
Thaddy
[May 13, 2026, 01:13:34 pm]
Hint tools gives wrong hi...
by
Nicole
[May 13, 2026, 01:11:47 pm]
[Solved] Scrollable, sing...
by
MathMan
[May 13, 2026, 01:00:08 pm]
Error: -macosx_version_mi...
by
Thaddy
[May 13, 2026, 11:59:25 am]
Stack overflow detection
by
Khrys
[May 13, 2026, 11:56:27 am]
« previous
next »
Print
Pages: [
1
]
Author
Topic: [Solved] SaveToFile with TMemoryStream (Read 1576 times)
jcmontherock
Sr. Member
Posts: 353
[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.6-64 - FPC 3.2.2
Fibonacci
Hero Member
Posts: 943
Behold, I bring salvation - FPC Unleashed
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
FPC Unleashed
- inline vars, tuples, statement expressions, array equality, compound assignments, indexed/lazy labels, no-RTTI & more. ⭐
Star it on GitHub!
jcmontherock
Sr. Member
Posts: 353
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.6-64 - FPC 3.2.2
Fibonacci
Hero Member
Posts: 943
Behold, I bring salvation - FPC Unleashed
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
FPC Unleashed
- inline vars, tuples, statement expressions, array equality, compound assignments, indexed/lazy labels, no-RTTI & more. ⭐
Star it on GitHub!
Print
Pages: [
1
]
« previous
next »
Lazarus
»
Forum
»
Free Pascal
»
General
(Moderators:
FPK
,
Tomas Hajny
) »
[Solved] SaveToFile with TMemoryStream
TinyPortal
© 2005-2018