Results 1 to 8 of 8

Thread: How read each line in a text file ?

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2000
    Location
    France
    Posts
    51

    Post How read each line in a text file ?

    Hello everybody

    I want my VB 5 Project to read a text file line by line to put each line in an array.


    Thank you very much if you can help me.

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Code:
    Private Sub Command1_Click()
    Dim ar() As String
    Dim i As Integer
    i = 1
    Open "c:\textfile.txt" For Input As #1
    Do While Not EOF(1)
    ReDim Preserve ar(0 To i) As String
    Line Input #1, ar(i)
    'ar contains lines
    i = i + 1
    Loop
    Close #1
    End Sub
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3
    Megatron
    Guest
    0 is the default base, you could have just wrote
    Code:
    ReDim ar(i)

  4. #4
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    You just love posting to get your postcount up, don't you, Megatron.
    Oops! I just did it too .

    So, I better post some content.

    You can also store files in a database-type storage, (one field though), and then retrieve/write using get/put.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  5. #5
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    why do you use "Preserve" when redimming the array?

  6. #6
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Each time you execute the ReDim statement, all the values currently stored in the array are lost. Visual Basic resets the values to the Empty value (for Variant arrays), to zero (for numeric arrays), to a zero-length string (for string arrays), or to Nothing (for arrays of objects).

    This is useful when you want to prepare the array for new data, or when you want to shrink the size of the array to take up minimal memory. Sometimes you may want to change the size of the array without losing the data in the array. You can do this by using ReDim with the Preserve keyword. For example, you can enlarge an array by one element without losing the values of the existing elements using the UBound function to refer to the upper bound:
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  7. #7
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527

    Thumbs up

    ok, so it basically just keeps the value in the array.

    thanks

  8. #8
    Megatron
    Guest
    You just love posting to get your postcount up, don't you, Megatron.
    No, I was correcting him on something.

    You can also store files in a database-type storage, (one field though), and then retrieve/write using get/put.
    For simple file name storage, arrays are much cheaper than using a Database.

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