Read and Write Binary String
Posted: Sat Aug 13, 2005 10:03 am
Code updated For 5.20+
A small exemple to show how to Write and read Structure with all type of variable inside.
Regards
Guimauve
A small exemple to show how to Write and read Structure with all type of variable inside.
Regards
Guimauve
Code: Select all
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Déclaration de la Structure >>>>>
Structure StructTest
Byte.b
Word.w
Long.l
Float.f
String.s
EndStructure
; <<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Les mutateurs >>>>>
Procedure SetStructTestByte(*ObjetA.StructTest, Byte.b)
*ObjetA\Byte = Byte
EndProcedure
Procedure SetStructTestWord(*ObjetA.StructTest, Word.w)
*ObjetA\Word = Word
EndProcedure
Procedure SetStructTestLong(*ObjetA.StructTest, Long.l)
*ObjetA\Long = Long
EndProcedure
Procedure SetStructTestFloat(*ObjetA.StructTest, Float.f)
*ObjetA\Float = Float
EndProcedure
Procedure SetStructTestString(*ObjetA.StructTest, String.s)
*ObjetA\String = String
EndProcedure
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Les observateurs >>>>>
Procedure.b GetStructTestByte(*ObjetA.StructTest)
ProcedureReturn *ObjetA\Byte
EndProcedure
Procedure.w GetStructTestWord(*ObjetA.StructTest)
ProcedureReturn *ObjetA\Word
EndProcedure
Procedure.l GetStructTestLong(*ObjetA.StructTest)
ProcedureReturn *ObjetA\Long
EndProcedure
Procedure.f GetStructTestFloat(*ObjetA.StructTest)
ProcedureReturn *ObjetA\Float
EndProcedure
Procedure.s GetStructTestString(*ObjetA.StructTest)
ProcedureReturn *ObjetA\String
EndProcedure
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< L'opérateur Update >>>>>
Procedure UpdateStructTest(*ObjetA.StructTest, Byte.b, Word.w, Long.l, Float.f, String.s)
SetStructTestByte(*ObjetA, Byte)
SetStructTestWord(*ObjetA, Word)
SetStructTestLong(*ObjetA, Long)
SetStructTestFloat(*ObjetA, Float)
SetStructTestString(*ObjetA, String)
EndProcedure
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Procedure WriteBinaryString(String$)
WriteLong(0,Len(String$))
WriteData(0,@String$, Len(String$))
EndProcedure
Procedure.s ReadBinaryString()
Length = ReadLong(0)
String$ = Space(Length)
ReadData(0,@String$, Length)
ProcedureReturn String$
EndProcedure
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
NewList Liste.StructTest()
String.s = "Good Morning !"
For Compteur = 0 To 5
AddElement(Liste())
UpdateStructTest(@Liste(), Byte.b, Word.w, Long.l, Float.f, String.s)
Byte = Byte + 1 + Compteur
Word = Word + 5 + Compteur
Long = Long + 10000 + Compteur
Float = 3.141592 + Compteur
String = "Hello World no. " + Str(Compteur) + "!"
Next
Debug "We put some stuff into the linked list"
ForEach Liste()
Debug GetStructTestByte(@Liste())
Debug GetStructTestWord(@Liste())
Debug GetStructTestLong(@Liste())
Debug GetStructTestFloat(@Liste())
Debug GetStructTestString(@Liste())
Next
Debug "We create the Binary file"
If CreateFile(0, "Test.bin")
ForEach Liste()
WriteByte(0,GetStructTestByte(@Liste()))
WriteWord(0,GetStructTestWord(@Liste()))
WriteLong(0,GetStructTestLong(@Liste()))
WriteFloat(0,GetStructTestFloat(@Liste()))
WriteBinaryString(GetStructTestString(@Liste()))
Next
CloseFile(0)
EndIf
ClearList(Liste())
Debug "Test to check if the linked list is Empty : "
Debug "Number of element = " + Str(ListSize(Liste()))
Debug "We load the file"
If OpenFile(0,"Test.bin")
Repeat
AddElement(Liste())
SetStructTestByte(@Liste(),ReadByte(0))
SetStructTestWord(@Liste(),ReadWord(0))
SetStructTestLong(@Liste(),ReadLong(0))
SetStructTestFloat(@Liste(),ReadFloat(0))
SetStructTestString(@Liste(),ReadBinaryString())
Until Eof(0)
EndIf
Debug "We show content loaded from the file."
ForEach Liste()
Debug GetStructTestByte(@Liste())
Debug GetStructTestWord(@Liste())
Debug GetStructTestLong(@Liste())
Debug GetStructTestFloat(@Liste())
Debug GetStructTestString(@Liste())
Next