Results 1 to 4 of 4

Thread: get file data into array?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    Say I have a text file with the following contents:

    George::John::Megan
    Bridget::Mike::Chris

    The :: is obviously the delimiter.

    I would then want to store each name into an array such
    as:

    Array(0)
    Array(1)
    Array(2)

    any help would be appreciated..

    Dan


  2. #2
    Addicted Member
    Join Date
    Aug 2000
    Posts
    208
    I can help you for the first part:

    dim input1 as string

    open "c:\windows\file.txt" for input as #1
    inputline #1,temp$
    input1=temp$
    close#1
    text1.text=replace(input1,"::",vbcrlf)

    now, you have all your stuff in a text box, one name per line..
    You juste need to know how to separate the name by line and put in a array...



  3. #3
    Lively Member
    Join Date
    Aug 2000
    Location
    Texas
    Posts
    88
    This will solve your problem....

    Code:
        Dim arr() As String
        Dim strTmp As String
        
        Open "Testfile.txt" For Input As #1
        While Not EOF(1)
            Line Input #1, strTmp
            arr = Split(strTmp, "::")
            
            'Do something about arr array here....
            '......
            
        Loop
        Close #1

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

    <?>

    same as above uses split to build the array
    but reads the complete file and not line by line
    as the above with line input is only reading one
    line and then creating the array.It will continually
    overwrite itself.

    [code]
    'your file must be amended to add :: after each name
    George::John::Megan::
    Bridget::Mike::Chris::
    '
    saved as C:\my documents\mytext.txt

    'read the complete file instead of line by line
    'faster and cleaner

    Option Explicit

    Private Sub Command1_Click()
    Dim sResult as variant 'must be variant
    Dim myVar as string
    Dim sfile As String, intNum As Integer
    intNum = FreeFile
    sfile = "C:\my Documents\myfile.txt"

    Open sfile For Input As intNum
    myVar = StrConv(InputB(LOF(intNum), intNum), vbUnicode)
    Close intNum

    '
    sResult = Split(myVar,"::")
    'you now have an array sResult
    '
    'to check it out
    Dim i as Integer
    For i = lbound(sResult) to (uBound(sResult)- 1)
    Msgbox sResult(i)
    Next i
    '
    End Sub
    [/code}

    [Edited by HeSaidJoe on 09-10-2000 at 03:43 PM]
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

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