Announcement

Collapse
No announcement yet.

Problem reading UDT as strings from binary file

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem reading UDT as strings from binary file

    Searching for a way of storing a variable number of different array (both strings & UDTs) and a couple of non-array UDTs, I have loaded each row of each array into a string (not asciz) , preceded by an identifying character. The file resulting from this looks convincing.

    Reloading the file looks like this:
    Code:
        hfile = FREEFILE
        OPEN sAppName FOR BINARY AS #hfile
        FILESCAN #hfile, RECORDS TO nrecs
        DIM sTemp(0 TO nrecs - 1)  ' sTemp is a string array
        GET #hfile, 1, sTemp()
        CLOSE #hfile
        DIM gCA(%lotsandlots) AS GLOBAL tCPH
        DIM gEB(%lotsandlots) AS GLOBAL STRING
            FOR i = 0 TO UBOUND(sTemp)
            SELECT CASE LEFT$(sTemp(i),1)
                CASE "F"
                    POKE$ VARPTR(Fstuff), MID$(sTemp(i),1) ' Fstuff is a UDT
                CASE "C"
                    POKE$ VARPTR(gCA(lcx)), MID$(sTemp(i),1)
                    INCR lcx
                CASE "E"
                    gEB(lex) = MID$(sTemp(i),1)
                    INCR lex
            END SELECT
        NEXT
        REDIM PRESERVE gCA( 0 TO lcx) AS GLOBAL tCPH
        REDIM PRESERVE gEB( 0 TO lex) AS GLOBAL STRING
    and it does just what I wanted, except that everything is offset "left" by one byte, so that a string member of a UDT array will have a leading zero byte, for example.

    Where am I going wrong?

  • #2
    Code:
    OPEN sAppName FOR BINARY AS #hfile
        FILESCAN #hfile, RECORDS TO nrecs
        DIM sTemp(0 TO nrecs - 1)  ' sTemp is a string array
        GET #hfile, 1, sTemp()
    GET help:
    With a dynamic string array, it is assumed the file was written in the PowerBASIC and/or VB packed string format using PUT of an entire string array. If a string is shorter than 65535 bytes, a 2-byte length WORD is followed by the string data.
    FILESCAN has a similar caveat.

    You may be running into length words and/or array bound violations, the latter of course can cause all kinds of weird things to happen.

    Of course, "CODE [which stores the data] NOT SHOWN" so this may not be a consideration at all.
    Michael Mattias
    Tal Systems Inc. (retired)
    Racine WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Originally posted by Chris Holbrook View Post
      I have loaded each row of each array into a string (not asciz) , preceded by an identifying character
      like this:
      Code:
      '        "C" is an array identifier in this case
              sTemp(n) = "C" + PEEK$(VARPTR(gCA(i)), SIZEOF(tCPH))
      '       etc, then
      ....
              put# hfile, sTemp()
      To dig out the code from the application (which is what I've resorted to) is taking a few moments...

      Comment


      • #4
        Duh!

        forgetting that mid$(s,1) copies from the first character of the string, not the second. Will they ever learn?

        Comment

        Working...
        X