none
从文本中读取矩阵,赋值给数组要怎么操作 RRS feed

  • 问题

  •  

    'address 要求读的文本的位置
    'arr(3,3) 要被赋值的矩阵
    '
    ——————————————
    Public Class Form1
        Private fileReader As String,arr(3,3) as integer, address As String
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            address = "C:\Users\Administrator\Desktop\text.txt"
            fileReader = My.Computer.FileSystem.(address, System.Text.Encoding.Default)
            
        End Sub
    End Class
    ——————————————
    文本里面的内容
    1 2 3
    4 5 6
    7 8 9
    ——————————————

    按照我上面的读法全读过来了,有没能有逐行读的方法,或者能把值赋过去的办法


     

    你好,我是VB9的,运行了一下你的代码提示有问题

    dim fields() as string  =line.split(" ")

    这里提示:不再支持“line”语句。文件I/O功能可用作“Microsoft.VisualBasic .FileSystem.LineInput”
    我改成dim fields() as string  =Microsoft.VisualBasic .FileSystem.LineInput()
    和dim fields() as string  =Microsoft.VisualBasic .FileSystem.LineInput.split(" ")
    都有提示:没有为"Public Function LineInput(FileNumber As Integer) As String"的参数"FileNumber"指定参数


    又改成Dim fields() As String = Microsoft.VisualBasic.FileSystem.LineInput(i).Split(" ")
    结果在运行时提示文件名或编号错误。




    最后自己写了一个能成功的,比较烦,分享给大家,希望大家能把它再改进一些

    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim fileReader As System.IO.StreamReader, arr(2, 2) As Integer, n(1) As Integer
            fileReader = My.Computer.FileSystem.OpenTextFileReader("C:\\Users\\Administrator\\Desktop\\test.txt", System.Text.Encoding.Default)
            Dim stringReader As String
            n(0) = 0
            Do
                stringReader = fileReader.ReadLine()
                If stringReader Is Nothing Then
                    Exit Do
                End If
                n(1) = 0
                For j = 2 To Len(stringReader)
                    '找到空格后把它前面的赋给数组,后面的变为新的字符串
                    If Mid(stringReader, j, 1) = " " Then
                        arr(n(0), n(1)) = Val(Microsoft.VisualBasic.Left(stringReader, j - 1))
                        stringReader = Microsoft.VisualBasic.Right(stringReader, Len(stringReader) - j + 1)
                        n(1) = n(1) + 1
                    End If
                Next
                '把最后剩下的转为数组的最后一个
                arr(n(0), n(1)) = Val(stringReader) & vbCrLf
                n(0) = n(0) + 1
            Loop
            For i = 0 To 2
                For j = 0 To 2
                    TextBox1.Text = TextBox1.Text & arr(i, j) & " "
                Next
                TextBox1.Text = TextBox1.Text & vbCrLf
            Next
        End Sub
    End Class

     

    • 已编辑 ww-air 2009年2月6日 8:13
    2009年2月5日 12:34

答案

  •   Private fileReader As String,arr(2,2) as integer, address As String    '3x3的数组  应该是 arr(2,2)
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            address = "C:\Users\Administrator\Desktop\text.txt"
            dim lines as string() = My.Computer.FileSystem.readalltext (address, System.Text.Encoding.Default).split(vbcrlf)
        
            for i as  integer =0 to lines.length -1 

                        dim fields() as string  =line.split(" ")

                        for  j as integer =0 to  fields.length -1
                                arr(i,j) =  cint(fields(j))
                            
                        next
            next
                    


        End Sub
    Guitar Hero IV -- Singing Rock & Roll.
    2009年2月5日 15:34

全部回复

  •   Private fileReader As String,arr(2,2) as integer, address As String    '3x3的数组  应该是 arr(2,2)
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            address = "C:\Users\Administrator\Desktop\text.txt"
            dim lines as string() = My.Computer.FileSystem.readalltext (address, System.Text.Encoding.Default).split(vbcrlf)
        
            for i as  integer =0 to lines.length -1 

                        dim fields() as string  =line.split(" ")

                        for  j as integer =0 to  fields.length -1
                                arr(i,j) =  cint(fields(j))
                            
                        next
            next
                    


        End Sub
    Guitar Hero IV -- Singing Rock & Roll.
    2009年2月5日 15:34
  • 你想一行一行读,可以使用StreamReader.ReadLine 方法 啊

    详细说明和代码
    http://msdn.microsoft.com/zh-cn/library/system.io.streamreader.readline.aspx




    2009年2月6日 0:52
    版主
  • 你可以把所有line 替换成 别的 这里我写的是变量名  没想到line是保留的


    Guitar Hero IV -- Singing Rock & Roll.
    2009年2月6日 2:43