Announcement

Collapse
No announcement yet.

writing multiple-dimensioned arrays to file

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

  • writing multiple-dimensioned arrays to file

    I use the following function for single dimensioned arrays.

    Code:
    Function WriteFil(Ary() As String, ByVal OutFil As String) As Long
    'output array to file  - is this only good for a one-dim array
       Local hFile As Long
    
       If IsFile(OutFil) Then
          Kill OutFil
       End If
    
       hFile = FreeFile
       Open OutFil For Output As #hFile
       Print #hFile, Ary()      '  [B]<----------[/B]
       Close #hFile
    End Function
    I may have messed up something else, but I can't seem to get
    "PRINT #hFile, Ary()" to work on a multi-dimensioned array. Is there some special syntax I've missed?


    In the meantime, I've had to write a second routine that goes thru the array line-by-line...

    Yes, I went thru the Helpfile, even tried JOIN$, but I need TABs between fields and CRLFs between records...

    I'd love to see "JOIN$" or "PRINT #file, Ary()" syntax to do so.

  • #2
    Why do you not use the Binary mode ?
    Old QB45 Programmer

    Comment


    • #3
      Not sure what you're suggesting.

      Code:
         Open OutFil For Binary As #hFile
         Put$ #hFile, Join$(Ary(), $CrLf)
         Close #hFile
      but that puts a CRLF after each field, not record.

      Comment


      • #4
        John,
        just use the PUT statement as shown in the helpfile:


        Binary files:
        PUT [#] fNum&, [RecPos], Arr()
        ..
        When PUT is used on a binary file, the entire array specified by Arr() is written to the file.

        Comment


        • #5
          Thanks Paul, but it ain't good...

          Code:
             Open OutFil For Binary As #hFile
             Put #hFile, 1,Ary()
          I'm getting all kinds of extraneous characters in the file between records and where line-end chars should be...

          Comment


          • #6
            John,
            it's intended to be used like this:
            Code:
            'PBCC5 program
            FUNCTION PBMAIN() AS LONG
            
            DIM a&(10,10)
            FOR r& = 0 TO 10
                FOR c& = 0 TO 10
                    a&(r&,c&)= r&*100 + c&
                NEXT
            NEXT
            
            PRINT "Before"; a&(5,7)
            
            OPEN "arrayfile" FOR BINARY AS #1
            PUT #1,,a&()
            CLOSE
            'clear the array
            FOR r& = 0 TO 10
                FOR c& = 0 TO 10
                    a&(r&,c&)= 0
                NEXT
            NEXT
            
            PRINT "after clear"; a&(5,7)
            
            OPEN "arrayfile" FOR BINARY AS #1
            GET #1,,a&()
            CLOSE
            
            PRINT "After restore from file"; a&(5,7)
            
            WAITKEY$
            
            END FUNCTION
            Perhaps you're using it differently?

            Paul.

            Comment


            • #7
              John, What are you wanting to do with the resultant file?

              If you just want to read it back into another program of your making then

              open "file" for binary as hfile
              put# hfile,,array()
              close

              will dump the array to disk and

              get# hfile,,array()

              will load it back.

              If you want it in human readable form then you'll need to do something else.
              Neil Croft (cissp)

              Comment


              • #8
                [quote=John Montenigro;320130]I use the following function for single dimensioned arrays.

                Code:
                Function WriteFil(Ary() As String, ByVal OutFil As String) As Long
                'output array to file  - is this only good for a one-dim array
                   Local hFile As Long
                 
                   If IsFile(OutFil) Then '[COLOR=red]<<< Not necessary. Open for Output overwrites[/COLOR]
                      Kill OutFil             [COLOR=red]'       if it exists.[/COLOR]
                   End If
                 
                   hFile = FreeFile
                   Open OutFil For Output As #hFile
                   Print #hFile, Ary()      '  [B]<----------[/B]
                   Close #hFile
                End Function
                The file gets overwritten when Opened for Output. Not that it has anything to do with Array writing but just to keep my hand in.

                ============================
                Success is the ability
                to go from one failure
                to another
                with no loss of enthusiasm.
                Winston Churchill.
                ============================
                It's a pretty day. I hope you enjoy it.

                Gösta

                JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                Comment


                • #9
                  This example extends Paul's 'PUT / GET' code to show the 'Print / Line Input' method too..
                  Code:
                  'PBWin program
                  FUNCTION PBMAIN() AS LONG
                   
                    DIM a$(2,5,10)
                    FOR p& = 0 To 2
                      FOR r& = 0 TO 5
                        FOR c& = 0 TO 10
                            a$(p&,r&,c&)= " Cell Value: " + format$(p&*10000 + r&*100 + c&, "000000")
                        NEXT
                      NEXT
                    NEXT
                   
                    ? "Before save, a$(2,5,7): " + a$(2,5,7)
                    ' Write ascii file
                    OPEN "ArrayFileA" FOR OUTPUT AS #2
                    PRINT #2, a$()                ' Note. When used w/ numeric values, written as ASCII text equiv values
                    CLOSE #2                      '       Delimited w/ $CRLF (new line per data value)
                   
                   ' Write binary file
                    OPEN "ArrayFileB" FOR BINARY AS #1
                    PUT #1,,a$()                  ' Write entire array to file as single block
                    CLOSE                         ' Dynamic string data saved in PowerBASIC packed string format
                   
                    GOSUB ClearArray
                    ? "a$(2,5,7) after clear: " + a$(2,5,7)
                   
                    ' Read ascii file
                    OPEN "ArrayFileA" FOR INPUT AS #2
                    FileScan #2, Records To Rec&
                    LINE INPUT #2, a$()           ' Assign full line of text to each array element
                    CLOSE #2
                    ? "After restore from A file, a$(2,5,7): " + a$(2,5,7) + $CR + str$(Rec&) + " Records"
                   
                    GOSUB ClearArray              ' clear the array
                    ? "a$(2,5,7) after clear: " + a$(2,5,7)
                   
                    ' Read Bin file
                    OPEN "ArrayFileB" FOR BINARY AS #1
                    FileScan #1, Records To Rec&
                    GET #1,,a$()                  ' With a dynamic string array, it is assumed the file was written 
                    CLOSE #1                      ' in the PowerBASIC packed string format
                    ? "After restore from B file, a$(2,5,7): " + a$(2,5,7) + $CR + str$(Rec&) + " Records"
                  
                   EXIT FUNCTION 
                   
                   ClearArray:
                    FOR p& = 0 To 2
                      FOR r& = 0 TO 5
                        FOR c& = 0 TO 10
                            a$(p&,r&,c&)= ""
                        NEXT
                      NEXT
                    NEXT
                   RETURN
                   
                  END FUNCTION 
                  '------------------/
                  #IF 0
                   
                  Array data is stored in column major order ([URL]http://en.wikipedia.org/wiki/Row-major_order[/URL])
                   
                  A file data..
                   Cell Value: 000000(crlf)
                   Cell Value: 010000(cr..etc
                   Cell Value: 020000
                   Cell Value: 000100
                   Cell Value: 010100
                   Cell Value: 020100
                   Cell Value: 000200
                   Cell Value: 010200
                   Cell Value: 020200
                   Cell Value: 000300
                   Cell Value: 010300
                   Cell Value: 020300
                   Cell Value: 000400
                   Cell Value: 010400
                   Cell Value: 020400
                   Cell Value: 000500
                   Cell Value: 010500
                   Cell Value: 020500
                   Cell Value: 000001
                   Cell Value: 010001
                   Cell Value: 020001
                   Cell Value: 000101(crlf) etc..
                   
                  B file data..
                   Two byte 'length' is followed by the string data. e.g. '&h1300' (LSB,MSB ~ 19 chars)
                   13 00  Cell Value: 000000 13 00  Cell Value: 010000 13 00 etc..
                   
                  #ENDIF
                  Last edited by Dave Biggs; 1 Aug 2009, 07:40 PM. Reason: Edit to display number of retrieved records
                  Rgds, Dave

                  Comment


                  • #10
                    Code:
                    ClearArray:
                      FOR p& = 0 To 2
                        FOR r& = 0 TO 5
                          FOR c& = 0 TO 10
                              a$(p&,r&,c&)= ""
                          NEXT
                        NEXT
                      NEXT
                     RETURN
                    ==>

                    Code:
                    RESET A$()
                    Michael Mattias
                    Tal Systems (retired)
                    Port Washington WI USA
                    [email protected]
                    http://www.talsystems.com

                    Comment


                    • #11
                      > Reset a$()

                      Yes that's better!
                      Rgds, Dave

                      Comment


                      • #12
                        One little potential gotcha here....

                        It does not matter in this demo because the array is never resized, but in this code...

                        Code:
                        ' Read Bin file
                          OPEN "ArrayFileB" FOR BINARY AS #1
                          GET #1,,a$()
                        .. the array already has to be sized properly. That is, 'as written' this code will not be useful for retrieving data saved in a previous program run unless you know the number of dimensions and the number of elements in each dimension.

                        I did a generic save/load array to disk thing a long time ago (and I mean a LONG time ago.. like back in the MS-DOS days). Maybe I can find that and figure out if there might be a way with the newer compilers to deal with that.

                        MCM
                        Michael Mattias
                        Tal Systems (retired)
                        Port Washington WI USA
                        [email protected]
                        http://www.talsystems.com

                        Comment


                        • #13
                          Thanks for all the options and suggestions, guys. I'm away from my development machine till Monday night, so I'll start checking code ideas then.

                          To answer the question about intended use: The file will be imported into a DBMS.
                          IF it's possible, a format that could be quickly scanned by grey-ware would be a plus.

                          -John

                          Comment


                          • #14
                            .. the array already has to be sized properly. That is, 'as written' this code will not be useful for retrieving data saved in a previous program run unless you know the number of dimensions and the number of elements in each dimension.
                            That's true. As is, the file data is saved in a linear fashon.
                            There would need to be information in the data itself to inform the user of more than just the number of records - which you can get with Filescan (for string arrays).

                            - Editted code above (#9) to show number of records retrieved from file.
                            Last edited by Dave Biggs; 1 Aug 2009, 07:42 PM.
                            Rgds, Dave

                            Comment

                            Working...
                            X