文本文件格式如下:
125
fdf
548
4t5
4555
d
48
要求,把文件中符合000--999的字符串存到一个数组里,数组的长度由符合条件的字符串个数决定,其他不符合条件如字母,小于3个或大于3个的字符串都忽略掉.

解决方案 »

  1.   

    呵呵,很基础的问题,看看MSDN完全可以自己解决:)
      

  2.   

    Dim Temp as string,arrResult() as string,i%
    i=1
    open "文件路径及名称" for input As #1
        Line Input #1, Temp
        if Temp<"000" And Temp>"999" then
            redim Preserve arrResult(i)
            i=i+1
        End if
    Close #1
      

  3.   

    条件写错了:Dim Temp as string,arrResult() as string,i%
    i=1
    open "文件路径及名称" for input As #1
        Line Input #1, Temp
        if Temp>="000" And Temp<="999" then
            redim Preserve arrResult(i)
            i=i+1
        End if
    Close #1
      

  4.   

    i%是什么意思,不好意思,我是个菜鸟,temp 是怎么放到arrResult里的
      

  5.   

    刚刚试了一下,数组里是空的,数组长度是对的和符合条件的个数一致,我在redim Preserve arrResult(i)和i=i+1中间加了一句arrResult(i) = Temp,最后,数组里的数不对,是怎么回事
      

  6.   

    Private Sub Command2_Click()
    Dim filestring As String, arrResult() As String, i As Integer, linestring As String
    filestring = App.Path & "\a.txt"
    Dim ofs As FileSystemObject
    Set ofs = New FileSystemObject
    Dim txtStream As TextStream
    Set txtStream = ofs.OpenTextFile(filestring)  '打开文件
    i = 1Do Until txtStream.AtEndOfStream    '按行读出
        linestring = txtStream.ReadLine
        If IsNumeric(linestring) = True And (Asc(linestring) >= Asc("000") And Asc  (linestring) <= Asc("999")) Then   '判断条件
            ReDim Preserve arrResult(1 To i) As String  '扩大数组
            arrResult(i) = linestring    '写入数组
            i = i + 1
        End If
          
    Loop
    txtStream.Close   ‘关闭流
    Set ofs = Nothing   ‘关闭对象
    Exit SubEnd Sub
      

  7.   

    (Asc(linestring) >= Asc("000") And Asc  (linestring) <= Asc("999")) Then   '判断条件
    这个条件是不是不对呀,我按你的方法,结果把小于3位的大于3位的都读了进去,只过滤掉了字母
      

  8.   

    问题解决了,我把你的条件换成了If IsNumeric(linestring) = True And Len(linestring) = 3  ,这样就能正确的读出了, faysky2() 朋友的那个方法哪个地方有问题,代码挺短的.顺便再问一下,把那个数组里的数一行一行的写入另一个文本文件应该用什么方法.是用writeline吗
      

  9.   

    Do Until txtStream.AtEndOfStream
        linestring = txtStream.ReadLine
        If IsNumeric(linestring) = True And Len(linestring) = 3 Then
            If CInt(linestring) >= 0 And CInt(linestring) <= 999 Then
                ReDim Preserve arrResult(1 To i) As String
                arrResult(i) = linestring
                i = i + 1
            End If
        End If
    Loop这样选出来的结果是 125,548
    IsNumeric(linestring) = True  '删除包含字母的
    len(linestring) = 3   '显示三位字符串
      

  10.   

    问题完全解决,自己再搞一下把读出的数按行写到另一个文本文件里,clear_zero(清晰)朋友可以再给个思路吗,这个好象比读简单点.明天中午给分可以吗.
      

  11.   

    目前你已经把符合条件的string 存储在你的array里面了。
    如果想写入另一个文本文件,步骤如下
    1.写入已存在文本文件还是自己创建 (指定路径)
    2.打开目标文本文件成写入状态
    3.对数组作循环,逐条写入
    4.关闭文件具体代码可以自己试探着来。当你定义了 FileSystemObject,很多的函数都可以看到了。
    自己写完了代码才可以真正的懂
      

  12.   

    Dim Temp As String, x() As String, num As Integer
    Private Sub Command1_Click() 'read
    num = 0
    Open "c:\xxx.txt" For Input As #1
    Do While Not EOF(1)
        Line Input #1, Temp
        If IsNumeric(Temp) And Len(Temp) = 3 And Temp > "0" Then
        num = num + 1
            ReDim Preserve x(1 To num)
           x(num) = Temp
        End If
    Loop
    Close #1
    End SubPrivate Sub Command2_Click() 'write
    Open "c:\newfile.txt" For Output As #1
    Print #1, , Join(x, vbCrLf)
    Close #1
    End Sub
      

  13.   

    or use binary style:Dim Temp As String, x() As String, num As Integer
    Private Sub Command1_Click() 'read
    Dim y() As String, i As Integer
    Open "c:\xxx.txt" For Binary As #1
    Temp = Space(LOF(1))
    Get #1, , Temp
    Close #1
    y = Split(Temp, vbCrLf)
    For i = 0 To UBound(y)
    Temp = y(i)
        If IsNumeric(Temp) And Len(Temp) = 3 And Temp > "0" Then
        num = num + 1
            ReDim Preserve x(1 To num)
           x(num) = Temp
        End If
    Next
    Close #1
    Erase y
    End SubPrivate Sub Command2_Click() 'write
    Open "c:\newfile.txt" For Binary As #1
    Put #1, , Join(x, vbCrLf)
    Close #1
    End Sub
      

  14.   

    啊,高手真多啊,有这么多方法真是受益非浅啊,可惜我的分数太少了,都不知道该怎么分了,你们每个人写的代码都对我有启发,非常感谢,我就都给点分,第一个解决问题的clear_zero(清晰) 多给点:)然后是northwolves(狼行天下),有了大家的热情帮助我想我的vb会突飞猛进的:)
      

  15.   

    northwolves(狼行天下) 用你的方法写到另一个文件里,为什么写入的文件第一个数字之前有一个很长的空格,数了一下14个