Results 1 to 7 of 7

Thread: Read an Array

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2000
    Posts
    8
    Does anyone understand how to read an array and then place into a text file for later use?

    I need to create an array that will loop through a series of numbers and then place those numbers into a text file for later use.

    Please help,

    Thanks

    Rey

  2. #2
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    Text1.text = Array(1) is that what you needed

  3. #3
    Guest
    This will save an Array.
    Code:
    'Create the Array
    Dim MyArray(5) As Byte
    MyArray(0) = "23"
    MyArray(1) = "45"
    MyArray(2) = "22"
    MyArray(3) = "1"
    MyArray(4) = "4"
    MyArray(5) = "43"
    
    'Save the Array
    Open "MyFile.txt" For Output As #1
        For I = LBound(MyArray) To UBound(MyArray)
            Print #1, MyArray(I)
        Next I
    Close #1

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    If you open your file in binary, your file will be much smaller, useful especially if you have large arrays.

    Code:
    dim ff as integer
    ff=freefile
    Open file for binary as ff
      put#ff,,Arrayname
    close ff
    If you use dynamic arrays, you should put the dimensions before the array so that you can redimension it when you need to load it back from the file.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    ...my 2 cents

    Code:
    Option Explicit
    
    Private Sub Command2_Click()
    'read them into a file
    
    Dim myArr(1 To 20)
    Dim i As Integer
    
    Dim intNum As Integer
    intNum = FreeFile
    
    Dim yourfile As String
    yourfile = "C:\my documents\myfile.txt"
    
    Open yourfile For Output As intNum
    Randomize
    For i = 1 To 20
      myArr(i) = i & CInt(Rnd * 40)
      Print #intNum, myArr(i)
     
    Next i
    Close intNum
    
    End Sub
    
    
    'read them from a file
    Private Sub Command1_Click()
    
    Dim myArr()
    Dim i As Integer
    'let's say you are stored in a file called your myfile
    
    Dim intNum As Integer
    intNum = FreeFile
    
    Dim yourfile As String
    
    yourfile = "C:\my documents\myfile.txt"
    
    Open yourfile For Input As intNum
    
    Do Until EOF(intNum)
        i = i + 1
        ReDim Preserve myArr(i)
        Input #intNum, myArr(i)
    
    Loop
      Close intNum
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  6. #6
    Fanatic Member Mad Compie's Avatar
    Join Date
    Aug 2000
    Location
    Kuurne (Belgium)
    Posts
    553
    HeSaidJoe, I say hi.
    Don't you think the ReDim statement is a little bit misplaced here in the Do...Loop iteration?
    I would ReDim (without Preserve) the table before the loop with a certain maximum, and after the file has been read a ReDim Preserve with the number of records read.
    That's how I would do it, I don't know if there's a difference...

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    The difference is that you could put anything into the file, but to redim it all the time would be slow, especially for large files, i recommend again you open in binary and get the array with get statement, write it with put.
    As i said To write the upperbound would be a solution
    Code:
     'To write
     Dim UB as integer
     UB=Ubound(Arrayname)
     Put #1,,UB
     Put #1,,Arrayname
     'To read
     Get #1,,UB
     Redim Arrayname(UB)
     Get #1,Arrayname
    note that you would have to use integer, if you have arrays larger than 255, if smaller you could use byte, but to be sure with huge arrays use long
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

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