怎样用vb读取文本中的字符,比如又如下字符:
1 hhhhh                      6.185100e+002       4.431515e+005       2.879233e+005       1.105589e+004       9.597442e+003       1.110178e+004       9.597442e+003       0.000000e+000       0.000000e+000       0.000000e+000       0.000000e+000       0.000000e+000       0.000000e+000 
 51    1        40.083        -5.500        40.083        -3.900        38.883        -3.200 
读取第一行到第三行的 字符,从第一行的第3个字符开始,把读取的数值放在数值a()中,读取第4行的第1个数,第2个数。
读取第4行的第3个数到最后一个数。
这个该如何读取呀!
大家帮忙帮我看看,谢谢啦!

解决方案 »

  1.   

    按行读取,然后自己去判断
    dim Str as stringopen 文件 for input as #1
    while(1)
    readline #1,Str
    Str判断语句wend
      

  2.   

    Option Explicit
        Dim strfFleName
    Private Sub cmdOpen_Click() '确定文本文件的总行数
        Dim str As String
        Dim str1 As String
        Dim NextLine As String
        Dim Txt_sj As String
        Dim l As Integer
        CommonDialog1.ShowOpen
        Text1 = ""
        strfFleName = CommonDialog1.FileName
        Open strfFleName For Input As #1
            Do While Not EOF(1)
                Line Input #1, str1
                str = str & str1 & Chr(13) & Chr(10)
                l = l + 1
            Loop
        Close
        Text1 = str
        Text3 = l '行数
    End SubPrivate Sub Command1_Click() '查看文本文件的指定行的内容
        Dim str As String
        Dim str1 As String
        Dim NextLine As String
        Dim Txt_sj As String
        Dim l As Integer
        Dim n As Integer
        n = InputBox("读取行", n)
        'CommonDialog1.ShowOpen
        Open strfFleName For Input As #1
            Do While Not EOF(1)
                Line Input #1, str1
                str = str & str1 & Chr(13) & Chr(10)
                l = l + 1
                If l >= n Then Exit Do
            Loop
        Close
        Text3 = l
        Text5 = "第" & l & "行的内容:" & str1 'Right(str1, Len(str1) - 5) '查看文本文件的指定行第5字符后的内容
    End Sub
    上述是读取指定行的代码,读取后用Split函数来返回一个下标从零开始的一维数组,它包含指定数目的子字符串,然后用Val 函数来获取包含于字符串内的数字(字符串中是一个适当类型的数值)。
      

  3.   

    楼上的说得对,这就是两行数。不好意思没说明白,请大家见谅。我再详细说一下。
    1 hhhhh                      6.185100e+002      4.431515e+005      2.879233e+005      1.105589e+004      9.597442e+003      1.110178e+004      9.597442e+003      0.000000e+000      0.000000e+000      0.000000e+000      0.000000e+000      0.000000e+000      0.000000e+000 
    51    1        40.083        -5.500        40.083        -3.900        38.883        -3.200 就是第一行的数就是从“1 hhhhh  ”开始到“ 0.000000e+000 ”结束。
    我要读的是第一行“1 hhhhh  ”后面的数值。即从“ 6.185100e+002   ”开始到“ 0.000000e+000 ”  结束的数值,把这组数值放在数组a()里。字符和数值之间是空格隔开。1和“hhhhh ”空一格,“hhhhh ”和“ 6.185100e+002   ”之间空12格。后面的数值之间空8格
    同样的,第二行从“51   1”开始,“51   1”之间空3格。“1        40.083 ”之间空10格,“40.083”往后的数值之间都是空5格。
    第二行读第一个数51,第二个数1,分别用变量x,y表示。从第3个数值开始,往后所有的数都放在数组b()中。
    大概就是这样的读取格式。
    没有说明白的地方恳请大家见谅!
     
      

  4.   

    Dim StrTmp As String
    Dim TextLine As String
    Dim strAry() As String
    dim AArr() As String
    dim BArr() As String
       Open App.Path & "\XX.ini" For Input As #1 ' 打开文件。
       Do While Not EOF(1) ' 循环至文件尾
         Line Input #1, TextLine ' 读入一行数据并将其赋予某变量。
         StrTmp = StrTmp & TextLine & Chr(13)
       Loop
        Close #1 ' 关闭文件。
    strAry=split(StrTmp,Chr(13))
    AArr=split(Replace(strAry(0)," ",""),".")'去掉空格,并以“.”分段
    j=0
    for i=1 to ubound(AArr)
      a(j)=CDbl(right(AArr(i)),11)
      j++
    next
    x=clng(BArr(0))
    y=clng(BArr(1))
    j=0
    for i=2 to ubound(BArr)
      b(j)=CCur(right(BArr(i)),11)
      j++
    next
      

  5.   

    不知道是不是这样 Private Sub Command1_Click()Dim arr() As String, a() As String, b() As String, X As String, Y As String
    Dim i As Long, s As Long
    Dim strA As String, ResultA As String, ResultB As String
     Open "c:\1.txt" For Binary As #1
     strA = Space(LOF(1))
     Get #1, , strA
     Close #1
       
         strA = Trim(strA)
        Do While InStr(strA, "  ") <> 0  '不管有多少个连续的空格,统统替换成一个空格
          strA = Replace(strA, "  ", " ")
        Loop
       
       arr = Split(strA, vbCrLf)
        For i = 0 To 0     '第一行
          a = Split(arr(i), " ")
            For s = 2 To UBound(a)
              If a(s) <> "0.000000e+000" Then
                ResultA = ResultA & vbCrLf & a(s)
              End If
            Next
         Next
         MsgBox ResultA   '第一行结果
         
        arr = Split(strA, vbCrLf)
       For i = 1 To 1
         a = Split(arr(i), " ")
            X = a(0)
            Y = a(1)
          For s = 3 To UBound(a)
            ResultB = ResultB & vbCrLf & a(s)
          Next
        Next
        
         MsgBox X
         MsgBox Y
         MsgBox ResultB
    End Sub
      

  6.   

    Option ExplicitPrivate Sub Command1_Click()    Dim a#(), b#(), x%, y%
        Dim t$(), strTemp$, i&, nF&, m&
        
        nF = FreeFile()
        Open "X:\Temp\Num.txt" For Input As #nF
        Line Input #nF, strTemp
        While (InStr(1, strTemp, "  ") > 0)
            strTemp = Replace(strTemp, "  ", " ")
        Wend
        t = Split(Trim$(strTemp))
        m = UBound(t) - 2
        ReDim a(m)
        For i = 0 To m
            a(i) = Val(t(i + 2))
        Next
        Line Input #nF, strTemp
        While (InStr(1, strTemp, "  ") > 0)
            strTemp = Replace(strTemp, "  ", " ")
        Wend
        t = Split(Trim$(strTemp))
        m = UBound(t) - 2
        ReDim b(m)
        x = Val(t(0))
        y = Val(t(1))
        For i = 0 To m
            b(i) = Val(t(i + 2))
        Next
        Close nFEnd Sub
      

  7.   

    Dim strLine As String, strItems() As String, i As Integer
    Dim a() As Double, b() As Double, x as Integer, y As IntegerOpen "文本.txt" For Input As #1
    '读第一行
    Line Input #1, strLine
    '去除多余空格
    Do While Instr(strLine, Space(2))
        strLine = Replace(strLine, Space(2), Space(1)
    Loop
    '分割
    strItems = Split(strLine, Space(1))
    '移入数组
    Redim a(Ubound(strItems) - 2)
    For i = 0 To Ubound(a)
        a(i) = CDbl(strItems(i + 2))
    Nex i'读第二行
    Line Input #1, strLine
    '去除多余空格
    Do While Instr(strLine, Space(2))
        strLine = Replace(strLine, Space(2), Space(1)
    Loop
    '分割
    strItems = Split(strLine, Space(1))
    '赋值
    x = Val(strItems(0))
    y = Val(strItems(1))
    Redim b(Ubound(strItems) - 2)
    For i = 0 To Ubound(b)
        b(i) = CDbl(strItems(i + 2))
    Nex i