例如我有一个数组a(i,j),已经保存在一个文件里面,现在要读取它,首先应该确定它的行和列吧,然后要具体读取每个元素,该怎么作?请赐教!

解决方案 »

  1.   

    那就在保存数组前先保存i,j,  读的时候先把i,j读出来,redim后再读后面的数组。
      

  2.   

    得到TXT文件的行数:
    Private Sub Command1_Click()
        Dim lineNO As Long,temp As String
        Open "c:\test1.txt" For Input As #1
        Do While Not EOF(1)
            Line Input #1, temp
            lineNO=lineNO+1
        Loop
        Close #1
        Debug.Print lineNO  '打印行数
    End Sub
      

  3.   

    如果你的文件内容是这个1,1,2,3,4
    2,1,2,3,4
    3,1,2,3,4
    4,1,2,3,4
    Private Sub Command3_Click()
        Dim s() As String
        Dim s1() As String
        Dim ss() As String
        Dim Index As Long
        Dim LineID As Long
        Dim CharID As Long
        Dim sLine As String
        Dim sChar As String
        Dim i, j As Long
        
        Open App.Path & "\1.txt" For Input As #1
        Index = 0
        Do While Not EOF(1)
            ReDim Preserve s(Index)
            Line Input #1, s(Index)
            Index = Index + 1
        Loop
        Close #1
        
        For i = 0 To UBound(s)
            s1 = Split(s(i), ",")        ‘如果你是用","作为分割符
           ReDim Preserve ss(UBound(s), UBound(s1))
            For j = 0 To UBound(s1)
                ss(i, j) = s1(j)
            Next j
        Next i
    End Sub
      

  4.   

    如果你存的时候各个元素是以","隔开的,那么可以这样做:
    Private Sub Command1_Click()
        Dim strOut As String, arrOut As Variant
        Open "c:\test1.txt" For Input As #1
        Do While Not EOF(1)
            Line Input #1, temp
            strOut = strOut & temp & vbCrLf
        Loop
        Close #1
        arrOut = Split(strOut, vbCrLf)    Dim i%,j%,Temp As Variant,arrResult As Variant,nRow As Long,nCol As Long
        Temp=Split(arrOut(0),",")'假设你保存数组到TXT文件的时候是以","来分隔各个元素
        nRow=Ubound(arrOut)-1
        nCol=Ubound(Temp)-1    redim arrResult(nRow,nCol) '重新定义2维数组大小
        for i=0 to nRow
            Temp=Split(arrOut(i),",")
            for j=0 to nCol
                arrResult(i,j)=Temp(j)
            next
        next
    End Sub