Announcement

Collapse
No announcement yet.

Array Elements -Saving to File

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

  • Array Elements -Saving to File

    First: I am new to programming. I have been writing a program as a
    teaching tool. I am wanting to save the elements from the below arrays
    to a random access file.

    DIM propclt$(NumberofProperties%)
    DIM propaddress$(NumberofProperties%)
    DIM propowner$(NumberofProperties%)
    DIM propownermail$(NumberofProperties%)
    DIM propownerphone$(NumberofProperties%)

    From looking at the help files and various books, I see references to
    *reading* random access files but nothing that I have seen directly
    refers to writing data from array's to the random access file.

    Can anyone please get me back on course...

    Thanks for any help provided,

    Ray.



    ------------------

  • #2
    Unfortunately, there is no native way to do this. Additionally, for dynamic strings, you cannot write raw blocks of memory to disk because dynamic strings are not stored "sequentially" in memory.

    The solution is to use a standard FOR/NEXT loop, writing each string element in succession.
    Code:
    OPEN "datafile.dat" FOR OUTPUT AS #1
    FOR x% = LBOUND(Arr1$,1) TO UBOUND(Arr1$,1)
      PRINT #1, Arr1$(x%)
    NEXT x%
    CLOSE #1
    PS: PB/CC 3.0 does offer a way to write whole arrays to a file with a single statement (and read them back just as easily!), but there is no equivalent function in PB/DOS.

    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    Lance
    mailto:[email protected]

    Comment


    • #3
      Code:
      TYPE MyArrayStructure
        Phone  AS STRING * 10
        Person AS STRING * 30
      END TYPE
      CLS
      DIM Record AS MyArrayStructure
      
      LengthOfStructure = 40
      
      NumberOfProperties& = 3
      DIM PhoneArray$(NumberOfProperties&)
      DIM PersonArray$(NumberOfProperties&)
      
      FileNum = FREEFILE
      OPEN "TEST.DAT" FOR RANDOM AS #FileNum LEN = LengthOfStructure
      
      FOR Count& = 1 TO NumberOfProperties&
        PhoneArray$(Count&) = "PHONE" + STR$(Count&)    'create some data
        PersonArray$(Count&) = "PERSON" + STR$(Count&)  'data in arrays
      
        Record.Phone = PhoneArray$(Count&)
        Record.Person = PersonArray$(Count&)
      
      
        PUT #FileNum, Count&, Record
      
      
        GET #FileNum, Count&, Record
        PRINT Record.Phone, Record.Person
      NEXT
      CLOSE #FileNum

      [This message has been edited by Mike Doty (edited December 10, 2002).]
      The world is full of apathy, but who cares?

      Comment


      • #4
        Mike,

        Thank you for your post. Do I TYPE and DIM in the main module or the
        subprogram that is doing the writing to the file or does it matter?

        Thanks,

        Ray.


        [QUOTE]Originally posted by Mike Doty:

        TYPE MyArrayStructure
        Phone AS STRING * 10
        Person AS STRING * 30
        END TYPE

        DIM Record AS MyArrayStructure


        ------------------

        Comment

        Working...
        X