Results 1 to 2 of 2

Thread: Reading info from a file

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2000
    Location
    Medford,MA,USA
    Posts
    23
    I have a txt(say text1.txt) file that has information in the following format:


    How are you
    Hello
    What a beautiful day
    1/25/00

    I would like to input each line into an array. How can I do this?
    This is not working:

    Dim A As String
    Open "text1" For input As #1
    Line input #1, A$
    myarray(0) = A


    I am getting the first word in the first line stored in the array instead of the entire line. Also how can I process all the lines into each array? I would like to have it as follows:

    myarray(0) = "How are you"
    myarray(1) = "Hello"
    myarray(2) = "What a beautiful day"
    myarray(3) = "1/25/00"

    Any help greatly appreciated
    Zack

  2. #2
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Mobile, AL, USA
    Posts
    600
    Hi tzachari,

    Here's how to do it.

    In order to use this code, put a command button and a list box on a form. Also, note that the path to the text file is: "c:\windows\desktop\text1.txt".
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        Dim myArray() As String
        Dim iCounter  As Integer
        Open "c:\windows\desktop\text1.txt" For Input As #1
        
        'Load the information into an Array
        Do Until iCounter = 4
            ReDim Preserve myArray(iCounter)
            Line Input #1, myArray(iCounter)
            iCounter = iCounter + 1
        Loop
        
        'Display the results
        For iCounter = 0 To 3
            List1.AddItem myArray(iCounter)
        Next
        
        MsgBox "All Done"
        
        Close #1
    End Sub
    All the best.

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