Structure problem

Just starting out? Need help? Post your questions and find answers here.
kake26
Enthusiast
Enthusiast
Posts: 157
Joined: Sun Jan 25, 2004 7:21 pm
Contact:

Structure problem

Post by kake26 »

Hi all,

Okay the following has vexed me enough so I think I'll ask here. I am attempting to emulate whats known as typed files, think pascal. Basically, the best way to describe it is as if I to a structure, put stuff in it and dumped the binary to a file. I've got it figured minus finding the proper size of the structure. Below is a example of what I have that seems to work.

Code: Select all

Structure test
    name.s
    moreinfo.s 
    id.w 
  EndStructure

tests.test\name = "Aurthor"
tests.test\moreinfo = "Dent" 
tests.test\id = 42

If CreateFile(0, "info.txt")           
    WriteData(0, @tests, 1024)  ; problem is determining the proper size of @tests        
    CloseFile(0)                         
Else
    ; It exploded
EndIf
I know doing the reverse from file to usable structure will be another issue, but the above is what I have so far.
- chris -
New User
New User
Posts: 9
Joined: Sun Jun 06, 2010 10:43 am

Re: Structure problem

Post by - chris - »

Code: Select all


Structure test
  name.s{10}
  moreinfo.s{10}
  id.w
EndStructure

tests.test\name = "Aurthor"
tests.test\moreinfo = "Dent"
tests.test\id = 42

test2.test

Debug SizeOf(test)

If CreateFile(0, "info.txt")
  WriteData(0, @tests, SizeOf(test))  ; problem is determining the proper size of @tests
  CloseFile(0)
Else
  ; It exploded
EndIf

If ReadFile(0, "info.txt")
  ReadData(0, @test2, SizeOf(test))
  CloseFile(0)
EndIf

Debug test2\name
Debug test2\moreinfo
Debug test2\id

PB v5.62 x86/x64
Windows 10 Pro
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

Re: Structure problem

Post by Guimauve »

Or this way if you don't want to be blocked by the fixed string length .

Code: Select all

Structure test
  
  name.s
  moreinfo.s 
  id.w 
  
EndStructure
  
Procedure WriteTest(FileID, *TestA.Test)
  
  WriteLong(FileID, Len(*TestA\name))
  WriteData(FileID, @*TestA\name, Len(*TestA\name))
  WriteLong(FileID, Len(*TestA\moreinfo))
  WriteData(FileID, @*TestA\moreinfo, Len(*TestA\moreinfo))
  WriteWord(FileID,  *TestA\id)
  
EndProcedure 
  
Procedure ReadTest(FileID, *TestA.Test)
  
  *TestA\name = Space(ReadLong(FileID))
  ReadData(FileID, @*TestA\name, Len(*TestA\name))
  *TestA\moreinfo = Space(ReadLong(FileID))
  ReadData(FileID, @*TestA\moreinfo, Len(*TestA\moreinfo))
  *TestA\id = ReadWord(FileID)
  
EndProcedure 
  
tests.test\name = "Aurthor"
tests.test\moreinfo = "Dent" 
tests.test\id = 42
  
If CreateFile(0, "info.txt")    
  
  WriteTest(0, tests)    
  CloseFile(0)
  
Else
  ; It exploded
EndIf
  
  
If ReadFile(1, "info.txt")   
  
  ReadTest(1, TestFromFile.Test)
  CloseFile(1)
  
Else
  ; It exploded
EndIf
  
Debug TestFromFile\name
Debug TestFromFile\moreinfo
Debug TestFromFile\id
Best regards.
Guimauve
kake26
Enthusiast
Enthusiast
Posts: 157
Joined: Sun Jan 25, 2004 7:21 pm
Contact:

Re: Structure problem

Post by kake26 »

Thank you both. I had a hunch I was close and it would seem that I was fairly close. In any case once more thanks for showing me how to do this.
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

Re: Structure problem

Post by Guimauve »

A little update to solve problem about Unicode string.

Compiler
--> Compiler options
--> Create unicode executable

Best ragards
Guimauve

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : Read/Write Complex Structure on file
; File Name : ReadWrite Complex Structure of File.pb
; File version: 1.0.0
; Programmation : OK
; Programmed by : Guimauve
; Date : 21-06-2010
; Update : 21-06-2010
; PureBasic code : 4.50
; Plateform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Procedure WriteStringEx(FileID.l, String.s)
  
  CompilerIf #PB_Compiler_Unicode
    WriteLong(FileID, 2 * Len(String))
    WriteData(FileID, @String, 2 * Len(String))
  CompilerElse 
    WriteLong(FileID, Len(String))
    WriteData(FileID, @String, Len(String))
  CompilerEndIf 
  
EndProcedure 
  
Procedure.s ReadStringEx(FileID.l)
  
  String.s = Space(ReadLong(FileID))
  ReadData(FileID, @String, Len(String))
  
  ProcedureReturn String
EndProcedure 
  
Structure test
  
  name.s
  moreinfo.s 
  id.w 
  
EndStructure
  
Procedure WriteTest(FileID, *TestA.Test)
  
  WriteStringEx(FileID, *TestA\name)
  WriteStringEx(FileID, *TestA\moreinfo)
  WriteWord(FileID,  *TestA\id)
  
EndProcedure 
  
Procedure ReadTest(FileID, *TestA.Test)
  
  *TestA\name = ReadStringEx(FileID)
  *TestA\moreinfo = ReadStringEx(FileID)
  *TestA\id = ReadWord(FileID)
  
EndProcedure 
  
tests.test\name = "Aurthor"
tests.test\moreinfo = "Dent" 
tests.test\id = 42
  
If CreateFile(0, "info.txt")    
  
  WriteTest(0, tests)    
  CloseFile(0)
  
Else
  ; It exploded
EndIf
  
If ReadFile(1, "info.txt")   
  
  ReadTest(1, TestFromFile.Test)
  CloseFile(1)
  
Else
  ; It exploded
EndIf
  
Debug TestFromFile\name
Debug TestFromFile\moreinfo
Debug TestFromFile\id
  
; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
Post Reply