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
Writing a structure to a file
-
- User
- Posts: 83
- Joined: Mon May 16, 2005 4:19 pm
- Location: St. Louis, MO
Writing a structure to a file
John R. Duchek
St. Louis,MO
St. Louis,MO
Re: Writing a structure to a file
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)
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

- StarBootics
- Addict
- Posts: 1006
- Joined: Sun Jul 07, 2013 11:35 am
- Location: Canada
Re: Writing a structure to a file
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 :
Best regards
StarBootics
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
StarBootics
The Stone Age did not end due to a shortage of stones !
Re: Writing a structure to a file
■ Save list to JSON File
■ 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()
;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)
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
➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti
Sorry for my bad english and the Dunning–Kruger effect
Re: Writing a structure to a file
Sorry I misread your request
■ With an Array.

■ 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)
➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti
Sorry for my bad english and the Dunning–Kruger effect