Page 1 sur 1

Sauvegarder les données d'une liste chaînée

Publié : mer. 09/juil./2008 23:26
par Octavius
Je voudrais une méthode rapide pour sauvegarder une liste chaînée puis la re-remplir à partir de la sauvegarde, pourquoi ce code ne marche pas ?

Code : Tout sélectionner

Structure Struct
  String$
  Number.l
EndStructure

NewList List.Struct()

AddElement(List())
List()\String$="Bidule"
List()\Number=259

AddElement(List())
List()\String$="Truc"
List()\Number=-33

ResetList(List())
While NextElement(List())
  Debug List()\String$
  Debug List()\Number
Wend

;*****************************************************

CreateFile(0,"file.bin")

FirstElement(List())
*Address=@List()
WriteData(0,*Address,SizeOf(Struct)*2)

CloseFile(0)

;*****************************************************

ClearList(List())

ReadFile(0,"file.bin")

ReadData(0,*Address,SizeOf(Struct)*2)

CloseFile(0)

ResetList(List())
While NextElement(List())
  Debug List()\String$
  Debug List()\Number
Wend

Publié : jeu. 10/juil./2008 0:29
par lionel_om
Si tu fais :

Code : Tout sélectionner

Debug SizeOf(Struct)
Ca va t'afficher 8.
Normal car ton entier fait 4 octets et le pointeur vers ton String : 4 aussi.
Donc ton chiffre sera bien enregistré, mais pas ta chaîne.

Faut donc faire:

Code : Tout sélectionner

WriteLong(0, List()\Number)
WriteStringN(0, List()\String)
Et lors du chargemet, faire de la même manière. Désolé, mais pour les Strings et les structures, on ne peut pas simplifier la chose...

/Lio

Publié : jeu. 10/juil./2008 3:30
par Guimauve
Bonjour,

Moi je fais plutôt comme ceci :

Code : Tout sélectionner

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; CODE GÉNÉRÉ AUTOMATIQUEMENT, NE PAS MODIFIER À
; MOINS D'AVOIR UNE RAISON TRÈS TRÈS VALABLE !!!
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Code généré par : Dev-Type V1.2.0
; Nom du projet : Le nom du projet ici
; Nom du fichier : Nom du fichier
; Version du fichier : 0.0.0
; Programmation : À vérifier
; Programmé par : Votre Nom Ici
; Alias : Votre Pseudo Ici
; Courriel : adresse@quelquechose.com
; Date : 09-07-2008
; Mise à jour : 09-07-2008
; Codé pour PureBasic V4.10
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Déclaration de la Structure <<<<<

Structure Struct
   
   String.s
   Number.l
   
EndStructure

; <<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Les mutateurs <<<<<

Macro SetStructString(StructA, P_String)
   
   StructA\String = P_String
   
EndMacro

Macro SetStructNumber(StructA, P_Number)
   
   StructA\Number = P_Number
   
EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Les observateurs <<<<<

Macro GetStructString(StructA)
   
   StructA\String
   
EndMacro

Macro GetStructNumber(StructA)
   
   StructA\Number
   
EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< L'opérateur Update <<<<<

Macro UpdateStruct(StructA, P_String, P_Number)
   
   SetStructString(StructA, P_String)
   SetStructNumber(StructA, P_Number)
   
EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< L'opérateur Reset <<<<<

Macro ResetStruct(StructA)
   
   SetStructString(StructA, "")
   SetStructNumber(StructA, 0)
   
EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Read/Write BinaryString <<<<<

Procedure.s ReadBinaryString(FileID)
   
   length.l = ReadLong(FileID)
   String.s = Space(length)
   ReadData(FileID, @String, length)
   
   ProcedureReturn String
EndProcedure
 
Procedure WriteBinaryString(FileID, String.s)
   
   length.l = Len(String)
   WriteLong(FileID, length)
   WriteData(FileID, @String, length)
    
EndProcedure 

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Lecture fichier Binaire <<<<<

Procedure ReadStruct(FileID.l, *StructA.Struct)
   
   SetStructString(*StructA, ReadBinaryString(FileID))
   SetStructNumber(*StructA, ReadLong(FileID))
   
EndProcedure

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Écriture fichier Binaire <<<<<

Procedure WriteStruct(FileID.l, *StructA.Struct)
   
   WriteBinaryString(FileID, GetStructString(*StructA))
   WriteLong(FileID, GetStructNumber(*StructA))
   
EndProcedure

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Lecture d'une liste chaînée dans le fichier en cours <<<<<

Procedure ReadLinkedListOfStruct(FileID.l, LList.Struct())
   
   LList_Max.l = ReadLong(FileID)
   
   For Index = 0 To LList_Max - 1
      AddElement(LList())
      ReadStruct(FileID, LList())
   Next
   
EndProcedure

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Écriture d'une liste chaînée dans le fichier en cours <<<<<

Procedure WriteLinkedListOfStruct(FileID.l, LList.Struct())
   
   WriteLong(FileID, CountList(LList()))
   
   ForEach LList()
      WriteStruct(FileID, LList())
   Next
   
EndProcedure

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Macro de déboguage <<<<<

Macro DebugStruct(StructA)
   
   Debug GetStructString(StructA)
   Debug GetStructNumber(StructA)
   
EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Code généré en : 00.031 secondes <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

NewList List.Struct()

AddElement(List())
UpdateStruct(List(), "Bidule", 259)

AddElement(List())
UpdateStruct(List(), "Truc", -33)

AddElement(List())
UpdateStruct(List(), "Alpha", 115)

Debug "------------------------------------------"
Debug "Mise en place des éléments dans la liste"
Debug "------------------------------------------"

ForEach List()
   DebugStruct(List())
Next

If CreateFile(0,"file.bin")
   WriteLinkedListOfStruct(0, List())
   CloseFile(0)
EndIf

ClearList(List())

Debug "---------------------------------------"
Debug "Test de lecture sur fichier binaire"
Debug "---------------------------------------"

If ReadFile(1,"file.bin")
   ReadLinkedListOfStruct(1, List())
   CloseFile(1)
EndIf

ForEach List()
   DebugStruct(List())
Next

; <<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< FIN DU FICHIER <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<<<<
A+
Guimauve

Publié : jeu. 10/juil./2008 11:40
par brossden
plus simple je pense :

Code : Tout sélectionner

Structure Struct
  String$
  Number.l
EndStructure

NewList List.Struct()

AddElement(List())
List()\String$="Bidule"
List()\Number=259

AddElement(List())
List()\String$="Truc"
List()\Number=-33

ResetList(List())
ForEach List()
  Debug List()\String$
  Debug List()\Number
Next

;*****************************************************

CreateFile(0,"c:\file.bin")
ForEach List()
  WriteStringN(0, List()\String$)
  WriteStringN(0, Str(List()\Number))
Next
CloseFile(0)

;*****************************************************

ClearList(List())

OpenFile(0,"c:\file.bin")
While Not Eof(0)
  AddElement(List())
  List()\String$ = ReadString(0,#PB_Ascii  )
  List()\Number = Val(ReadString(0))
Wend

CloseFile(0)

Debug "*"
ForEach List()
  Debug List()\String$
  Debug List()\Number
Next