Lazarus
Home
Forum
Help
TinyPortal
Search
Login
Register
Lazarus
»
Free Pascal
»
Database
(Moderators:
FPK
,
Tomas Hajny
) »
BufDataSet - LoadFromFile problem
Free Pascal
Website
Downloads
Wiki
Bugtracker
Mailing List
Lazarus
Website
Downloads (Laz+FPC)
FAQ
Wiki
Bugtracker
Packages
IRC channel
Developer Blog
Follow us on Twitter
Latest SVN
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
Search
Advanced search
« previous
next »
Print
Pages: [
1
]
Author
Topic: BufDataSet - LoadFromFile problem (Read 2796 times)
HappyLarry
Full Member
Posts: 144
BufDataSet - LoadFromFile problem
«
on:
August 08, 2016, 10:51:49 am »
In BufDataSet, I can not get LoadFromFile to work.
I have tried both
Code: Pascal
[Select]
T
.
LoadFromFile
(
'Names.bds'
)
;
and
Code: Pascal
[Select]
T
.
Edit
;
T
.
LoadFromFile
(
'Names.bds'
)
;
T
.
Post
;
but neither shows the record.
All help gratefully received.
Here is my test program
Code: Pascal
[Select]
program
Loadtest
;
uses
db
,
BufDataset
;
var
T
:
TBufDataSet
;
begin
//Create a dataset, add a record and save it
T
:
=
TBufDataset
.
Create
(
nil
)
;
T
.
FieldDefs
.
Add
(
'Name'
,
ftString
,
20
)
;
T
.
CreateDataset
;
T
.
Append
;
T
.
Fields
[
0
]
.
value
:
=
'Fred'
;
T
.
Post
;
writeln
(
'Before Save :'
,
T
.
Fields
.
FieldByName
(
'Name'
)
.
AsString
)
;
T
.
SaveToFile
(
'Names.bds'
)
;
T
.
Free
;
//Create a dataset, load the file and show it
T
:
=
TBufDataset
.
Create
(
nil
)
;
T
.
FieldDefs
.
Add
(
'Name'
,
ftString
,
20
)
;
T
.
CreateDataset
;
//T.Edit;
T
.
LoadFromFile
(
'Names.bds'
)
;
//T.Post;
writeln
(
'After Load :'
,
T
.
Fields
.
FieldByName
(
'Name'
)
.
AsString
)
;
T
.
free
;
writeln
(
'------ done ----------'
)
;
readln
;
end
.
Logged
Use Lazarus and Free Pascal and stand on the shoulders of giants . . . very generous giants. Thank you.
howardpc
Hero Member
Posts: 2638
Re: BufDataSet - LoadFromFile problem
«
Reply #1 on:
August 08, 2016, 11:06:04 am »
LoadFromFile creates the data set.
All you want after saving the data set to file is this:
Code: Pascal
[Select]
//Create a dataset, load the file and show it
T
:
=
TBufDataset
.
Create
(
nil
)
;
T
.
LoadFromFile
(
'Names.bds'
)
;
writeln
(
'After Load :'
,
T
.
Fields
.
FieldByName
(
'Name'
)
.
AsString
)
;
T
.
free
;
Logged
ttomas
Full Member
Posts: 173
Re: BufDataSet - LoadFromFile problem
«
Reply #2 on:
August 08, 2016, 11:06:54 am »
This work OK
Code: Pascal
[Select]
//Create a dataset, load the file and show it
T
:
=
TBufDataset
.
Create
(
nil
)
;
//----> T.FieldDefs.Add('Name',ftString,20);
//----> T.CreateDataset;
//T.Edit;
T
.
LoadFromFile
(
'Names.bds'
)
;
Logged
wp
Hero Member
Posts: 4386
Re: BufDataSet - LoadFromFile problem
«
Reply #3 on:
August 08, 2016, 11:27:22 am »
Or do this:
Code: Pascal
[Select]
T
:
=
TBufDataset
.
Create
(
nil
)
;
T
.
FileName
:
=
'Names.bds'
;
T
.
Open
;
Having specified the filename will cause automatic saving of the file if the dataset is closed later.
Logged
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10
HappyLarry
Full Member
Posts: 144
Re: BufDataSet - LoadFromFile problem
«
Reply #4 on:
August 08, 2016, 02:22:40 pm »
@ All
Great answers - Thanks
Another problem: I have tried to write a procedure to load a text file. The text file record structure is (String, Integer, Real). I have written:
Code: Pascal
[Select]
procedure
LoadTextFile
(
Filename
:
string
;
var
T
:
TBufDataSet
)
;
var
MyFile
:
Text
;
C1
:
string
;
P1
:
integer
;
A1
:
real
;
begin
Assignfile
(
MyFile
,
FileName
)
;
Reset
(
MyFile
)
;
While
not
System
.
EOF
(
MyFile
)
do
begin
readln
(
MyFile
,
C1
)
;
readln
(
MyFile
,
P1
)
;
readln
(
MyFile
,
A1
)
;
T
.
AppendRecord
(
[
C1
,
P1
,
A1
]
)
;
end
;
CloseFile
(
MyFile
)
;
end
;
This works but is not generally usable, because it relies on the record structure being(String, Integer, Real),
Is there a way of writing it so that it works whatever the record structure, for instance if the record structure was (String, Integer, Integer, String);?
If it helps, here is the full program code.
Code: Pascal
[Select]
program
Loadtest
;
uses
db
,
BufDataset
;
var
T
:
TBufDataSet
;
procedure
SaveTextFile
(
Filename
:
string
;
T
:
TBufDataSet
)
;
var
j
:
integer
;
MyFile
:
text
;
begin
Assignfile
(
MyFile
,
FileName
)
;
Rewrite
(
MyFile
)
;
T
.
First
;
While
not
T
.
EOF
do
begin
For
j
:
=
0
to
T
.
FieldCount
-
1
do
begin
writeln
(
MyFile
,
T
.
Fields
[
j
]
.
Value
)
;
end
;
T
.
next
;
end
;
CloseFile
(
MyFile
)
;
end
;
procedure
LoadTextFile
(
Filename
:
string
;
var
T
:
TBufDataSet
)
;
var
MyFile
:
Text
;
C1
:
string
;
P1
:
integer
;
A1
:
real
;
begin
Assignfile
(
MyFile
,
FileName
)
;
Reset
(
MyFile
)
;
While
not
System
.
EOF
(
MyFile
)
do
begin
readln
(
MyFile
,
C1
)
;
readln
(
MyFile
,
P1
)
;
readln
(
MyFile
,
A1
)
;
T
.
AppendRecord
(
[
C1
,
P1
,
A1
]
)
;
end
;
CloseFile
(
MyFile
)
;
end
;
begin
//Create a dataset,
T
:
=
TBufDataset
.
Create
(
nil
)
;
T
.
FieldDefs
.
Add
(
'C'
,
ftString
,
20
)
;
T
.
FieldDefs
.
Add
(
'P'
,
ftInteger
)
;
T
.
FieldDefs
.
Add
(
'A'
,
ftFloat
)
;
//add a record
T
.
CreateDataset
;
T
.
Append
;
T
.
Fields
[
0
]
.
value
:
=
'Belgium'
;
T
.
Fields
[
1
]
.
value
:
=
11
;
T
.
Fields
[
2
]
.
value
:
=
32.5
;
T
.
Post
;
//Save
writeln
(
'Before Save :'
,
T
.
Fields
.
FieldByName
(
'C'
)
.
AsString
)
;
SaveTextFile
(
'Countries.txt'
,
T
)
;
//Clear Table
T
.
Delete
;
writeln
(
'After Delete :'
,
T
.
RecordCount
,
' records'
)
;
//Reload
LoadTextFile
(
'Countries.txt'
,
T
)
;
writeln
(
'After Load :'
,
T
.
Fields
.
FieldByName
(
'C'
)
.
AsString
)
;
T
.
free
;
writeln
(
'------ done ----------'
)
;
readln
;
end
.
Logged
Use Lazarus and Free Pascal and stand on the shoulders of giants . . . very generous giants. Thank you.
howardpc
Hero Member
Posts: 2638
Re: BufDataSet - LoadFromFile problem
«
Reply #5 on:
August 08, 2016, 02:52:49 pm »
For persistent data saved to a textfile you can organise your file so that the first line saved with SaveToText represents the field structure (fieldname, type, and length). Your LoadTextFile procedure would then parse the first line read as a special composite value whose individual parts enable you to reconstruct the correct TFieldDefs for your BufDataSet. Or you can save this information as a separate schema file which LoadTextFile opens and reads first.
Either way, once the correct database structure is determined, LoadTextFile can continue to read individual records using ReadLn, parsing each record into its constituent fields which are appended to the dataset in memory.
Note that best practice is to use TextFile (rather than Text) as the file type. If you ever migrate your code to a GUI app, use of Text will most likely produce unwelcome name clashes with the Text properties of numerous classes.
«
Last Edit: August 08, 2016, 06:58:52 pm by howardpc
»
Logged
Print
Pages: [
1
]
« previous
next »
Lazarus
»
Free Pascal
»
Database
(Moderators:
FPK
,
Tomas Hajny
) »
BufDataSet - LoadFromFile problem
Recent
Replicate CURL command wi...
by
lainz
[
Today
at 09:10:24 pm]
AutoSize TMemo.Height?
by
ASerge
[
Today
at 08:38:48 pm]
Advise on how to create a...
by
lainz
[
Today
at 08:03:39 pm]
how to disable directive
by
marcov
[
Today
at 07:29:25 pm]
[SOLVED]Have troubles che...
by
CM630
[
Today
at 07:28:47 pm]
TDBCombobox useless?
by
wwerner
[
Today
at 07:20:47 pm]
Strange problem
by
taazz
[
Today
at 06:48:16 pm]
FPHTTPClient + GitHub = :...
by
Phil
[
Today
at 05:42:48 pm]
Help conversion string UT...
by
critter
[
Today
at 05:21:01 pm]
Desabilitar componentes d...
by
arirod
[
Today
at 05:16:37 pm]