Page 1 of 1

File :: Readstructure (), WriteStructure () functions

Posted: Sun Jan 17, 2016 9:50 pm
by Lunasole
The VB6 had nice thing that PB still misses for some reason - writing whole structure to a file.

For example here is structure with dynamical string.

Code: Select all

Structure test 
	s.s
EndStructure
Define X.test
	X\s = "1234"
	
CreateFile(1, "D:\1.txt")
	WriteData(1, @X, SizeOf(X))
CloseFile(1)
It saves to file only pointer value of a string, which is useless.
If using same code with VB6 it saves and reads the whole string itself, not a pointer. Which is very useful and you don't have to write your own bloated module to pack every structure manually.

Code: Select all

Option Explicit
Private Type Test
    s As String
End Type


Private Sub Command1_Click()
    Dim X As Test
        X.s = "1234"

    Open "D:\1.txt" For Binary As 1
        Put 1, 1, X
    Close 1
    
    Dim Y As Test
    
    Open "D:\1.txt" For Binary As 1
        Get 1, 1, Y     ; not even size to read specified. Y is not exactly as X
    Close 1
    
    Debug.Print Y.s
End Sub

Re: File :: Readstructure (), WriteStructure () functions

Posted: Sun Jan 17, 2016 11:22 pm
by STARGÅTE
In case of Strings (Ascii, UTF8, Unicode), Integers (4Byte or 8Byte) and List/Map/Array-Headers, WriteStructure have to write also some headers, otherwise you can't read it again with ReadStructure. So some one have to define a format.

But than you can use the JSON Library with InsertJSONStructure().

Re: File :: Readstructure (), WriteStructure () functions

Posted: Sun Jan 17, 2016 11:24 pm
by Demivec
Perhaps using JSON. Example Link.

Re: File :: Readstructure (), WriteStructure () functions

Posted: Mon Jan 18, 2016 2:53 am
by Keya
+1! Saving to/from JSON is really quite handy and minimal code, but well... it's JSON text structure not binary, so it comes with both performance and filesize penalties (it even saves the variable names along with values) that i'm usually not interested in. I think WriteData(@structure, SizeOf(structure)) works only if there are no nested structures, so im not sure if its even possible for us users to implement an alternative in a practical sense... it seems something only the compiler could do?

Re: File :: Readstructure (), WriteStructure () functions

Posted: Mon Jan 18, 2016 3:28 am
by Lunasole
JSON might be used, but it is poor replacement of binary, slow and useless web-format as for me, even worst than classic .ini. So no, I wouldn't use it for such things (nor for store anything ^^), especially when need to save a lot of data.

STARGÅTE wrote:In case of Strings (Ascii, UTF8, Unicode), Integers (4Byte or 8Byte) and List/Map/Array-Headers, WriteStructure have to write also some headers, otherwise you can't read it again with ReadStructure. So some one have to define a format.
The string encoding and int sizes are not a problem, may be passed as params to a ReadStructure() / write.
The maps/list/arrays, yes, more complicated, they can be nested, recursive and so on, it might be harder to do. But anyway format to dump to file must be close to how PB stores them internally to avoid doing too much operations. Need to know enough how it handles maps/lists to tell something about format and how complicated could be to realize such stuff