开发者

Convert text file into 2d array in vb.net

I have a text file like

11111
10001
10001
11111

I need to read this into a 2d array of integers I already have the code to read the file

 Dim fullpath = "Path to File"
 Dim objReader As StreamReader
 objReader = New StreamReader(fullpath)

But i dont know what to do after that. I'm know this is somethi开发者_高级运维ng simple but i just cant think of it now -_-


I assume the 2d array is to store each individual digit in each individual row. Also assume we only have 4 lines of 5 numbers each. (Don't assume this, unless you know its forceable--otherwise calculate the necessary size and redim the array)

    Dim myArray(4, 5) As Integer, y As Integer = 0, x As Integer = 0
    Dim fullpath = "Path to File"

    Using sr As StreamReader = New StreamReader(fullpath )
            Do While sr.Peek() >= 0
                   For Each c As Char In sr.ReadLine
                       Try
                           myArray(x, y) = Integer.Parse(c)
                       Catch ex As Exception 'i assume this is the only possible error, but we could be out of bounds due to assuming the actual size of the file/line... catch specific exceptions as necessary'
                           Console.WriteLine(String.Format("Error converting {0} to an integer.", c))
                       End Try
                       y += 1
                   Next
                   x += 1
                   y = 0
            Loop
       End Using
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜