Page 1 of 1
Writing a structure to a file
Posted: Sun Mar 01, 2015 3:35 pm
by John Duchek
Is there a way to write a structure to a file? I have this structure defined.
Structure datatrak
filename.s
description.s
date_updated.s
num.l
wnt.l
mincond.l
EndStructure
Dim cn.datatrak(100)
and am interested in writing the array cn(100) to a file. From the instructions I have read, it looks like I have to convert it to strings or write each variable separately. Not much point in having a structure if true.
Is there a
write cn(x) command I am missing?
Thanks,
John
Re: Writing a structure to a file
Posted: Sun Mar 01, 2015 4:02 pm
by ts-soft
Use Fixed-Lenght strings:
Code: Select all
Structure datatrak
filename.s{100}
description.s{50}
date_updated.s{10}
num.l
wnt.l
mincond.l
EndStructure
Dim cn.datatrak(100)
Re: Writing a structure to a file
Posted: Sun Mar 01, 2015 4:31 pm
by StarBootics
As ts-soft suggest you can use fixed string but if you can't the only wat out is to create a custom command like these :
Code: Select all
Structure DataTrak
filename.s
description.s
date_updated.s
num.l
wnt.l
mincond.l
EndStructure
Procedure ReadDataTrak(FileID, *DataTrakA.DataTrak)
*DataTrakA\filename = ReadString(FileID)
*DataTrakA\description = ReadString(FileID)
*DataTrakA\date_updated = ReadString(FileID)
*DataTrakA\num = ReadLong(FileID)
*DataTrakA\wnt = ReadLong(FileID)
*DataTrakA\mincond = ReadLong(FileID)
EndProcedure
Procedure WriteDataTrak(FileID, *DataTrakA.DataTrak)
WriteStringN(FileID, *DataTrakA\filename)
WriteStringN(FileID, *DataTrakA\description)
WriteStringN(FileID, *DataTrakA\date_updated)
WriteLong(FileID, *DataTrakA\num)
WriteLong(FileID, *DataTrakA\wnt)
WriteLong(FileID, *DataTrakA\mincond)
EndProcedure
Best regards
StarBootics
Re: Writing a structure to a file
Posted: Sun Mar 01, 2015 5:27 pm
by falsam
■ Save list to JSON File
Code: Select all
;PB 5.30
Enumeration
#JSONFile
EndEnumeration
Structure Address
Actif.b
Name.s
County.i
EndStructure
NewList Contacts.Address()
;Create 2 contacts
AddElement(Contacts())
With Contacts()
\Actif = #True
\Name = "Wagner"
\County = 75
EndWith
AddElement(Contacts())
With Contacts()
\Actif = #True
\Name = "Hilton"
\County = 92
EndWith
;Create new, empty JSON data
CreateJSON(#JSONFile)
;Insert the specified List() into the given JSON value
InsertJSONList(JSONValue(#JSONFile), Contacts())
;Save the given JSON data to a file
SaveJSON(#JSONFile, "contacts.json")
;Compose the given JSON data into a string
Debug ComposeJSON(#JSONFile, #PB_JSON_PrettyPrint)
■ Load List from JSON File
Code: Select all
;PB 5.30
Enumeration
#JSONFile
EndEnumeration
Structure Address
Actif.b
Name.s
County.i
EndStructure
NewList Contacts.Address()
;Read a JSON File (Parse JSON data from a file)
LoadJSON(#JSONFile, "contacts.json", #PB_JSON_NoCase)
;Extract elements into the specified List().
ExtractJSONList(JSONValue(#JSONFile), Contacts())
;Debug
ForEach Contacts()
With Contacts()
Debug \Name + " Country: " + \County
EndWith
Next
Re: Writing a structure to a file
Posted: Sun Mar 01, 2015 5:42 pm
by falsam
Sorry I misread your request
■ With an Array.
Code: Select all
;PB 5.30
Enumeration
#JSONFile
EndEnumeration
Structure Person
Name.s
ForName.s
Age.w
EndStructure
Dim MyFriends.Person(100)
; Here the position '0' of the array MyFriend()
; will contain one person and it's own information
MyFriends(0)\Name = "Andersson"
MyFriends(0)\Forname = "Richard"
MyFriends(0)\Age = 32
;Create new, empty JSON data
CreateJSON(#JSONFile)
;Insert the specified Array() into the given JSON value
InsertJSONArray(JSONValue(#JSONFile), MyFriends())
;Save the given JSON data to a file
SaveJSON(#JSONFile, "contacts.json")
;Compose the given JSON data into a string
Debug ComposeJSON(#JSONFile, #PB_JSON_PrettyPrint)