分享

VB中用数组按行读取TXT文件

 hdzgx 2017-10-22

Private Sub Form_Load()

Dim i%, j%, a$(), s

Me.AutoRedraw = True
Me.Width = 4000: Me.Height = 8000
Open "c:\score.txt" For Input As #1
 i = 0
 While Not EOF(1)
     ReDim Preserve a$(i)
     Line Input #1, a(i)
     a(i) = Replace(a(i), Chr(34), "") '去掉双引号
     i = i + 1
 Wend
 Close #1

 '验证
 For j = 0 To i - 1
     s = Split(a(j), " ") '以空格分隔
     Print "姓名:" & s(0) & " 英文:" & s(1) & " 数学:" & s(2) & " 地理:" & s(3)
 Next j
End Sub

 

另一个程序:

Private Sub Command1_Click()
Dim wellindex As Integer
Dim filename As String
Dim wellname(), wellx(), welly(), wellz() As Variant
Dim i As Integer
 Me.Cmdialog1.Filter = "text file(*.txt)|*.txt"
 Me.Cmdialog1.FilterIndex = 2 
 Me.Cmdialog1.filename = "*.txt"
 Me.Cmdialog1.ShowOpen
 filename = Me.Cmdialog1.filename
 
 Open filename For Input As 1
 Input #1, wellindex
 ReDim wellname(1 To wellindex), wellx(1 To wellindex), welly(1 To wellindex), wellz(1 To wellindex)
 For i = 1 To wellindex ' 
      Input #1, wellname(i), wellx(i), welly(i), wellz(i)
      Print Space(10); wellname(i); wellx(i); welly(i); wellz(i) 
Next i
 Close 1
End Sub 

**********************************

Private Sub Form_Load()


Dim i%, j%, a$(), s


Me.AutoRedraw = True

Me.Width = 4000: Me.Height = 8000

Open App.Path & "\tpml.txt" For Input As #1

 i = 0

 While Not EOF(1)

     ReDim Preserve a$(i)

     Line Input #1, a(i)

    ' a(i) = Replace(a(i), Chr(34), "") '去掉双引号

     Print a(i)

     i = i + 1

 Wend

 Close #1


 '验证

 For j = 0 To i - 1

     s = Split(a(j), " ") '以空格分隔

   '  Print a(j)

 Next j

End Sub

********************************
​Private Sub Command1_Click()
    Dim fso As Object, ts As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.OpenTextFile(App.Path & "\tpml.txt", 1)
    Do While ts.AtEndOfStream <> True
        Text1.Text = Text1.Text & ts.ReadLine & vbCrLf
    Loop
    ts.Close
    Set ts = Nothing
    Set fso = Nothing
End Sub

Private Sub Command2_Click()
    Dim fso As Object, ts As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.CreateTextFile(App.Path & "\tpml.txt", True)
     Text1.Text = Replace(Text1.Text, Chr(34), "") '去掉双引号
    ts.Write Text1.Text
    ts.Close
    Set ts = Nothing
    Set fso = Nothing
End Sub

 



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多