[英]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 我需要将其读入2D整数数组中,我已经有了读取文件的代码
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 something 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.
还假设我们只有4行,每行5个数字。 (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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.