finn3y
finn3y

Reputation: 101

Reading from text file to array

I am trying to read a text file in line by line and add it to an array, current code and output are below. How would I read it in properly, i.e. get the actual text to read into the array, rather than the current output. (Console App version of VB.NET)

Code:

Sub Main()
    Dim file As String = "C:\path\to\file\textfile.txt"
    Dim quoteArray As New ArrayList
    FileOpen(1, file, OpenMode.Input)
    Do While Not EOF(1)
        quoteArray.Add(LineInput(1))
    Loop
    FileClose(1)
    Console.WriteLine(quoteArray)
    Console.ReadLine()
End Sub

Output:

System.Collections.ArrayList

Upvotes: 5

Views: 22643

Answers (3)

V. Wheeler
V. Wheeler

Reputation: 569

If you know the file is going to be reasonably small, you can do it faster like this:

Public Function FileAsArrayOfStrings(asPath As String) As String()
    Debug.Assert(System.IO.File.Exists(asPath), "asPath must be an existing file.")
    Dim lsaLines As String()
    lsaLines = System.IO.File.ReadAllLines(asPath)
    Return lsaLines
End Function

Upvotes: 0

JohnyL
JohnyL

Reputation: 7122

Use ReadLines:

The ReadLines and ReadAllLines methods differ as follows: When you use ReadLines, you can start enumerating the collection of strings before the whole collection is returned; when you use ReadAllLines, you must wait for the whole array of strings be returned before you can access the array. Therefore, when you are working with very large files, ReadLines can be more efficient.

Upvotes: 3

Visual Vincent
Visual Vincent

Reputation: 18310

Your code works, but you cannot print an entire array at once. You've got to iterate the array somehow and print each item separately or combine them into a single string.

Printing each item separately:

For Each Item As String In quoteArray
    Console.WriteLine(Item)
Next

Combining them to a single string using String.Join():

Console.WriteLine(String.Join(Environment.NewLine, quoteArray.ToArray(GetType(String))))

However what I don't understand is why you are writing in VB.NET, but are still using outdated functions and classes from the VB6 era:

  • ArrayList

  • FileOpen()

  • LineInput()

  • FileClose()

There are much better alternatives these days:

Or you can replace all the above with a regular array and a single call to File.ReadAllLines().

  1. StreamReader solution:

    Dim quoteList As New List(Of String)
    
    Using Reader As New StreamReader("C:\path\to\file\textfile.txt")
        While Reader.EndOfStream = False
            quoteList.Add(Reader.ReadLine())
        End While
    End Using
    
  2. File.ReadAllLines() solution:

    Dim quoteArray As String() = File.ReadAllLines("C:\path\to\file\textfile.txt")
    

Printing the list/array using a loop:

For Each Item As String In quoteArray
    Console.WriteLine(Item)
Next

Printing the list/array using String.Join():

Console.WriteLine(String.Join(Environment.NewLine, quoteArray))

(if you are using the quoteList solution just replace quoteArray with quoteList in these two examples)

Upvotes: 14

Related Questions