I have a textfile I need to open and I need to put each line into the array. What is the simplest way to add the elements/each line's data into the array while increasing the array as it reads each line? This is blowing my mind right now...

I thought about using a counter and then redim-ing the array (while preserving of course). If so, how would that be constructed? Thanks!

Dani AI

Generated

If you truly need an array, consider reading lazily and only materializing once at the end. File.ReadLines streams the file line-by-line (unlike ReadAllLines, which loads the entire file at once), so it is memory-friendly for large inputs. You can also normalize or filter as you go:

Dim path As String = "C:\path\file.txt"
Dim lines As String() =
    System.IO.File.ReadLines(path).
        Select(Function(s) s.Trim()).
        Where(Function(s) s <> "").
        ToArray()

This avoids repeatedly resizing an array. ReDim Preserve reallocates and copies the array each time, which is O(n^2) overall and gets slow as the file grows; see the VB reference for details on how Preserve copies elements ReDim Statement. For very large files, you can also process each line without creating an array at all: iterate File.ReadLines(path) and handle each line inside the loop, which keeps memory usage flat. If you need a specific text encoding (for example, UTF-8 with BOM or Windows-1252), use the encoding overload of ReadLines or open a StreamReader with the proper Encoding. Docs: File.ReadLines, and for resource cleanup when manually using readers, the VB Using statement.

Recommended Answers

All 3 Replies

Member Avatar for Member #857553

I would use a list(Of T)

Heres 2 ways you could do that.

'Add with list(Of T)
        Dim fStream As New System.IO.FileStream("c:\text.txt", IO.FileMode.Open)
        Dim sReader As New System.IO.StreamReader(fStream)

        Dim List As New List(Of String)
        Do While sReader.Peek >= 0
            List.Add(sReader.ReadLine)
        Loop

        'to go back to an array
        Dim thisArray As String() = List.ToArray

        fStream.Close()
        sReader.Close()


        'with Array
        fStream = New System.IO.FileStream("c:\text.txt", IO.FileMode.Open)
        sReader = New System.IO.StreamReader(fStream)

        Dim sArray As String()
        Dim Index As Integer = 0

        Do While sReader.Peek >= 0
            ReDim Preserve sArray(Index)
            sArray(Index) = sReader.ReadLine
            Index += 1
        Loop

        fStream.Close()
        sReader.Close()
Member Avatar for Member #857553

Keep in mind you can use a List(of T) the same way as an array.

'You can use the List(Of T) the same way as an array.
        Dim thisLine As String
        For i = 0 To List.Count - 1
            thisLine = List(i)
        Next

        For Each S As String In List
            thisLine = S
        Next


        'And if you wanted your array to be 1 based then you could
        'get rid of the index.
        Dim sArray(0) As String
        Do While sReader.Peek >= 0
            ReDim Preserve sArray(sArray.Length)
            sArray(UBound(sArray)) = sReader.ReadLine
        Loop

why not just:

Dim lines() As String = IO.File.ReadAllLines("C:\myFie.txt")
For Each line As String In lines
	'do what you want with each single line
Next
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.