5

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
7
  • The output is correct because you are not printing the contents of the array. But ArrayList is antiquated as are those legacy File methods. Try Flie.ReadAllLines after you read How to Ask and take the tour Commented Dec 27, 2017 at 16:11
  • Not sure what was wrong with the question if i'm honest. Why not edit it to show me what was wrong? Also your answer provides no help as there is little context or explanation as to where to how to implement it. @Plutonix Commented Dec 27, 2017 at 16:15
  • 3
    @JackFinney What question? Commented Dec 27, 2017 at 16:18
  • 1
    I am not sure what you are whining about. I did not DV your post if that is the issue. As for context, programming will require that you do research - as the NET Framework is immense and we cant follow you around forever to fill in your gaps. Type File.ReadAlllines() into the IDE, put the cursor on it, press F1 and Study. As for the suggestion to read How to Ask and take the tour - you havent as yet, so it is a good idea to do so, and I am loathe to post answers for those who havent. Ditto for posts that have been down voted. Have a good day. Commented Dec 27, 2017 at 16:21
  • 1
    A) You should worry about DVs - enough of them will limit your ability to post. B) the real problem is in printing not reading C) The IDE was also probably suggesting you dont use some of those legacy Filexxx methods - pay attention to it because it Knows Things. Commented Dec 27, 2017 at 16:30

3 Answers 3

14

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)

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for the answer, i'll have a go at implementing them. I'm using outdated functions and classes due to the way I learn VB.NET at college, we use quite an old programming companion (stupid, I know). I'll also have a go at using and researching the newer alternatives you added.
@JackFinney note that all the code you posted to read the file a line at a time can be replaced by that one last line of code.
@JackFinney : I linked the documentation to every new thing I mentioned. It is a good place to start reading (it usually includes examples too).
I missed it, sorry. It;s way better
@Plutonix : Now, now... Don't mock my way of writing. ;)
|
3

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.