Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 从文本文件初始化时在矩形数组中获取不同的值_Arrays_Vb.net_Grid_Numbers_Streamreader - Fatal编程技术网

Arrays 从文本文件初始化时在矩形数组中获取不同的值

Arrays 从文本文件初始化时在矩形数组中获取不同的值,arrays,vb.net,grid,numbers,streamreader,Arrays,Vb.net,Grid,Numbers,Streamreader,我试图使用Visual Studio 2013中的vb.NET从包含20x20数字网格(以空格分隔)的文本文件初始化二维数组。我已经调整了我的循环以正确工作,但是我从streamreader获得的值与文件中的值完全不同 下面是vb代码 Imports System.IO Module Module1 Dim grid(19, 19) As String Sub Main() Dim deskPath As String = Environment.GetFolderPath(Envir

我试图使用Visual Studio 2013中的vb.NET从包含20x20数字网格(以空格分隔)的文本文件初始化二维数组。我已经调整了我的循环以正确工作,但是我从streamreader获得的值与文件中的值完全不同

下面是vb代码

Imports System.IO
Module Module1
Dim grid(19, 19) As String

Sub Main()
    Dim deskPath As String = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
    Using upload As New StreamReader(deskPath & "\grid.txt")
        'Console.WriteLine(upload.ReadToEnd)

        For x As Integer = 0 To 19

            For y As Integer = 0 To 19
                grid(x, y) = upload.Read() '& upload.read()
                upload.Read()
            Next

        Next
    End Using

Using outfile As New StreamWriter(deskPath & "\Result1.txt")
        For y As Integer = 0 To 19
            For x As Integer = 0 To 19
                outfile.Write(grid(x, y))
                outfile.Write(" ")
            Next
            outfile.WriteLine()
        Next
    End Using
End Sub
End Module
这是源文件的内容

08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
这就是阵列正在接收的内容

48 56 56 13 32 32 57 50 48 50 55 50 32 32 32 51 48 52 54 52 
32 53 53 56 56 49 13 32 32 53 53 54 52 56 53 32 32 32 50 52 
50 32 32 32 51 52 50 48 53 13 32 32 50 57 48 51 48 54 32 32 
50 50 48 57 32 32 32 52 48 51 52 50 13 32 32 57 48 51 53 55 
32 53 56 51 51 57 49 32 32 32 55 50 50 51 54 13 32 32 53 52 
55 32 32 32 52 50 49 56 52 56 32 32 32 49 52 55 50 49 13 32 
51 55 55 51 32 32 32 54 55 56 54 53 53 32 32 32 49 52 56 48 
32 57 52 53 51 56 49 32 32 32 49 54 53 56 48 55 32 32 32 50 
53 32 32 32 51 53 53 51 51 56 32 32 32 51 52 53 52 53 54 32 
48 56 56 57 32 32 32 49 53 54 56 52 53 32 32 32 48 53 48 56 
32 10 52 49 53 49 55 32 32 32 54 51 54 57 53 56 32 32 32 53 
48 57 32 32 10 51 54 48 56 51 32 32 32 54 49 50 51 56 56 32 
48 52 57 57 50 32 32 10 51 54 48 51 51 32 32 32 53 50 51 52 
32 32 52 57 55 54 57 52 32 32 10 48 57 50 48 53 32 32 32 49 
53 57 32 32 32 55 52 52 52 48 55 32 32 10 54 51 50 48 49 32 
48 52 52 49 53 32 32 32 50 50 50 48 54 49 32 32 10 49 56 56 
32 32 53 52 50 55 50 50 32 32 32 57 57 51 51 55 54 32 32 10 
53 55 32 32 32 48 51 54 53 56 48 32 32 32 57 49 51 52 55 57 
48 56 50 55 52 32 32 32 49 52 54 54 55 51 32 32 32 50 48 56 
32 32 48 53 54 54 52 57 32 32 32 52 55 48 52 52 53 32 32 32 
如果值保存在错误的元素中,我尝试查找模式,更改数组的数据类型,甚至将streamreader强制转换为int(这只是给出了一个错误)。正如您可能已经注意到的,我连接了两个read()操作来初始化每个元素中的两个数字,但这给了我完全不同的(不可还原为2位错误值)4位值。这些

4856 1052 1310 5313 5749 3256 5032 5452 3257 5732 5149 3253 5432 5349 3248 5732 5150 3251 5732 5649 
4850 3252 4932 5350 1050 1310 4813 5548 3250 5132 5151 3253 5732 5352 3256 5132 5451 3252 5332 4954 
5050 3257 5732 5548 3251 5232 5150 1054 1310 5013 5753 3257 5332 4955 3253 5532 5751 3254 5232 5051 
5755 3252 4932 5753 3249 5532 5756 3250 5232 5049 1055 1310 5513 5356 3252 5632 5351 3255 5232 5355 
5156 3249 5132 5051 3255 5032 5649 3250 5332 5154 3249 5432 5654 1049 1310 5413 5457 3251 5432 4853 
4953 3256 5332 4852 3253 4832 5056 3254 5632 5051 3253 5732 5354 3256 5232 5656 1048 1310 5413 5352 
4848 3249 5732 5448 3254 5732 5452 3248 5332 4857 3250 5332 4848 3256 5032 5154 3252 4832 5048 1048 
5248 3253 5232 4949 3254 5132 5051 3254 5432 5553 3250 5032 5256 3254 5632 5456 3249 5732 5551 3255 
4848 3254 5732 5250 3256 5332 5455 3249 5132 4848 3255 5432 5153 3248 5132 5655 3255 5432 5153 3253 
5553 3256 5132 5457 3252 5032 4948 3250 5732 5554 3251 5332 5549 3257 5532 5355 3251 4932 5057 3255 
4852 3249 4932 5052 3257 5232 5054 3257 5432 5252 3254 4932 5657 3252 5332 5450 3250 5032 5556 3256 
4853 3252 4832 5456 3251 5332 5156 3254 5532 5048 3249 5532 4855 3254 5732 5048 3251 4832 5149 3253 
4855 3257 5532 5354 3253 5132 5248 3257 5532 5253 3257 5332 4853 3250 5432 5550 3249 5132 5748 3253 
5556 3252 5132 4849 3250 5132 5455 3251 5632 5153 3248 5632 5252 3255 5532 4851 3250 5632 4849 3254 
5350 3254 5632 5150 3252 5632 5357 3254 5632 4952 3256 5632 5252 3257 5532 5254 3257 5232 5552 3249 
4950 3252 4832 5354 3252 5432 5352 3248 5432 4848 3248 5232 5155 3249 5532 5151 3255 5032 5149 3257 
5348 3248 5132 5549 3250 5232 5548 3252 5132 5449 3254 4832 5252 3256 5032 5455 3249 5732 5257 3251 
5555 3253 5732 5155 3254 4832 5454 3257 5232 5151 3249 5532 5448 3253 5432 5254 3248 5732 5549 3252 
5749 3254 5132 4850 3251 5332 4956 3254 5632 5755 3249 5232 5049 3249 5432 5353 3252 5032 5256 3254 
4856 3248 5432 5154 3249 5532 5156 3252 5232 5152 3248 5232 5356 3255 5432 4950 3250 5532 5654 3252
当我尝试直接在控制台上从streamreader打印原始值时,它工作正常,但相同的方法不适用于初始化数组

还有想法,建议,什么都欢迎


谢谢你抽出时间

您是否阅读了
StreamReader.read
方法的文档?它以
整数
的形式返回单个字符,即
字符
的Unicode值。如果要读取文件中的实际数字,则必须读取子字符串,然后自己将其转换为
整数

这样的事情似乎更明智:

Dim grid(19, 19) As Integer

Using reader As New StreamReader("file path here")
    For i = 0 To grid.GetUpperBound(0)
        Dim numbers = reader.ReadLine().Split()

        For j = 0 To grid.GetUpperBound(1)
            grid(i, j) = Convert.ToInt32(numbers(j))
        Next
    Next
End Using

您至少看到了两个问题

首先,用于填充数组的代码和用于打印数组内容的代码的内部循环和外部循环的顺序不同。在第一种情况下,x在外环中变化,y在内环中变化。第二个设置回路在外回路中的y和内回路中的x各不相同。这似乎可以解释部分差异

更重要的是,我注意到,打印时数组中的值似乎是所读取字符的ASCII码的十进制表示形式

请注意,返回的整数值与输入流中下一个字符的ASCII值相对应。将其指定给字符串变量时,vb.net会将整数转换为字符串(就像使用integer-ToString()方法一样)。在这种情况下,字符串将包含最初读取的字符的ASCII码的十进制值,而不是实际读取的字符


有许多其他方法可以完成此任务。我建议的一种方法是使用TextReader一次读取一行中的输入文件,然后使用String.Split获取每个数字的字符串表示形式

哦,我明白了,这很有帮助。我没想到编码会是个问题。String.split()会很聪明的谢谢!