Results 1 to 23 of 23

Thread: Read/Write arrays of UDT in Binary Files

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    18

    Read/Write arrays of UDT in Binary Files

    In my program users can add and remove Label and image control at runtime from a picture wich is the container, so users can add label and image control as they want. I loop each control to determine each property and save it in a binary files, to later read this file and load each control.....
    Code:
    Public Type misText
        nombre As String
        indice As Long
        titulo As String
        alto As Long
        ancho As Long
        tope As Long
        izquierda As Long
        FontName As Variant
        FontSize As Variant
        FontBold As Variant
        FontItalic As Variant
        FontUnderline As Variant
        ForeColor As Variant
    End Type
    Public Type misImag
        nombre As String
        indice As Long
        alto As Long
        ancho As Long
        tope As Long
        izquierda As Long
        imagPath As String
    End Type
    Public cantText As Long 
    Public cantImg As Long
    Public miText() As misText
    Public miImg() As misImag
    
    Public Sub Guardando(sFic)
    Dim Imagenes()
    Dim Textos()
    Dim CodigoB
    Dim objCtl As Control
    cantText = 0
    cantImg = 0
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'Lo primero que hago es abrir el fichero y borrarlo, porque sino se anexa la info
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Open (MDIMain.CommonDialog1.FileName) For Binary As #1
    Close #1
    Kill (MDIMain.CommonDialog1.FileName)
    
        For Each objCtl In FrmDiseno.Controls
                
                If objCtl.Container Is FrmDiseno.Picture1 Then
                
                    Select Case objCtl.Name
                        Case "Texto1"
                            cantText = cantText + 1
                            ReDim Preserve miText(cantText)
                            miText(cantText).alto = objCtl.Height
                            miText(cantText).ancho = objCtl.Width
                            miText(cantText).izquierda = objCtl.Left
                            miText(cantText).tope = objCtl.Top
                            miText(cantText).titulo = objCtl.Caption
                            miText(cantText).FontBold = objCtl.Font.Bold
                            miText(cantText).FontItalic = objCtl.Font.Italic
                            miText(cantText).FontName = objCtl.Font.Name
                            miText(cantText).FontSize = objCtl.Font.Size
                            miText(cantText).FontUnderline = objCtl.Font.Underline
                            miText(cantText).ForeColor = objCtl.ForeColor
                        Case "Imagen1"
                            cantImg = cantImg + 1
                            ReDim Preserve miImg(cantImg)
                            miImg(cantImg).alto = objCtl.Height
                            miImg(cantImg).ancho = objCtl.Width
                            miImg(cantImg).imagPath = objCtl.Picture
                            miImg(cantImg).izquierda = objCtl.Left
                            miImg(cantImg).tope = objCtl.Top
                    End Select
                    
                    
                End If
            
        Next
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'Ahora guardo la informacion en el fichero (Save the information in files)
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
    Dim nFic As Integer
    nFic = FreeFile
        Open sFic For Binary As nFic
            Put nFic, , cantText
                For I = 1 To cantText
                    Put nFic, , miText(I)
                Next I
            Put nFic, , cantImg
                For I = 1 To cantImg
                    Put nFic, , miImg(I)
                Next I
            Put nFic, , myBarcode
    Close nFic
       
    End Sub
    My Question is... How could i read this info from the File???
    Thanks.....

  2. #2

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Read/Write arrays of UDT in Binary Files

    Create a UDT that has 2 items, a misText item and a misImag item. Write this item to the file. I'd include the number of controls in the For Each loop as one of the elements in the variable, and I'd add a variable for the name of the control.

    To read, just read the file back into the new UDT and set your control properties to the values in the UDT. You know the name of each control read, and you know the number of controls.

    (It's easiest, for debugging purposes, to maintain a single variable length in the file.)
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  4. #4
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Read/Write arrays of UDT in Binary Files

    You can read/write UDT arrays directly to a file using Binary Access Write and the Put statement, no need for parsing/looping, etc.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    18

    Re: Read/Write arrays of UDT in Binary Files

    If the Image control have a picture ...How could include the picture in the binary files????

    Thanks

  6. #6
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Read/Write arrays of UDT in Binary Files

    Quote Originally Posted by Yovy7410
    If the Image control have a picture ...How could include the picture in the binary files????

    Thanks
    If you have a UDT with a member that is a picture ie:

    vb Code:
    1. Type MYTYPE
    2.     strOne As String
    3.     picAPicture As StdPicture
    4. End Type
    5.  
    6. Private Type udtType() As MYTYPE

    That picture data should get written to the binary file as well simply with the Put statement.

    I've never tried it with a Picture in a UDT but try it, it should work.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    18

    Re: Read/Write arrays of UDT in Binary Files

    Thanks....I'll Try

  8. #8
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Read/Write arrays of UDT in Binary Files

    Take a look at this thread for an example I made for someone else some time ago:

    http://www.vbforums.com/showthread.p...oto=nextoldest

  9. #9
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Read/Write arrays of UDT in Binary Files

    Why not just write the UDT's to a database, then you can retrieve them any way you want.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    18

    Re: Read/Write arrays of UDT in Binary Files

    Because i want to associate this file with a given extension (.etq) in my program, so user can load it from the main App...

    Thanks every body....

  11. #11
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Read/Write arrays of UDT in Binary Files

    And you can't do that with a database?

  12. #12
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    372

    Re: Read/Write arrays of UDT in Binary Files

    to put the UDT into a binary w/o parsing, you will need fixed-length strings.
    instead of: nombre as string
    use: nombre as string * 55

    for a 55char wide string.
    using fixed length strings allow you to move the whole UDT to/from a disk instantly.
    but, if you don't use fixed length, who knows where the data will fall when put back into the UDT...

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    18

    Re: Read/Write arrays of UDT in Binary Files

    Yes randem i could do it with a DB, but the idea is a File, so user could save that information where ever they want....thanks...

    in the oder hand rnd me
    is necessary putting fixed-length strings in Binary access?

  14. #14
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Read/Write arrays of UDT in Binary Files

    Then if you want to use a flat file you are going to need keys that proceed each UDT to tell you which one it is and how long it is so that you can get to the next one. Not too hard do do.

  15. #15
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Read/Write arrays of UDT in Binary Files

    Or you need to include a header on your flat file to show where in the file each UDT is located (offsets).

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    18

    Re: Read/Write arrays of UDT in Binary Files

    Thanks randem....

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    18

    Re: Read/Write arrays of UDT in Binary Files

    Have some example about this???

  18. #18
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Read/Write arrays of UDT in Binary Files

    No examples but here is the procedure. You will create a fixed size header for your file. Example 256 bytes. In the 256 bytes you will define the offset to where you write your UDT and the type and size of your UDT.

    Let say you define 7 UDT's. Your header would look something like this

    072561340139080

    This would state that UDT no 7 is at offset 256 and is 134 bytes long.
    and UDT 01 is at offset 390 and is 80 bytes long.

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    18

    Re: Read/Write arrays of UDT in Binary Files

    Yes.... is a good one....thanks

  20. #20
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Read/Write arrays of UDT in Binary Files

    And I would suggest using binary data (as opposed to text) in your header to conserve data size.

  21. #21

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    18

    Re: Read/Write arrays of UDT in Binary Files

    Sure....

  22. #22
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Read/Write arrays of UDT in Binary Files

    Use something like this for your header. Each entry will be 5 bytes then you could use a smaller header if your UDT's are limited.
    Code:
    Private Type Header
         UDTType as byte
         Offset as long
         Length as long
    End Type

  23. #23
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Read/Write arrays of UDT in Binary Files

    You don't need to store the position of every UDT item in the array, just how many there are in the array.

    Did you not click the example I posted above?

    Quote Originally Posted by rnd me
    to put the UDT into a binary w/o parsing, you will need fixed-length strings.
    instead of: nombre as string
    use: nombre as string * 55

    for a 55char wide string.
    using fixed length strings allow you to move the whole UDT to/from a disk instantly.
    but, if you don't use fixed length, who knows where the data will fall when put back into the UDT...
    And you don't need to use fixed-length strings either.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width