从.txt文件VB 6.0填充数组

时间:2014-11-27 18:55:09

标签: arrays loops vb6 listbox eof

我输了,我尝试填充数组并收到类型不匹配

我正在尝试从一个文件填充4个数组

文本文档中有500行,每行包含由“,”

分隔的4种不同类型的数据

.txt文件格式示例--->

梨,苹果,葡萄,橙子 苹果,梨,橙,葡萄

等...

这是我的代码:

Private Sub CmdRun_Click()
Dim ticketid(500) As Variant
Dim theatreid(500) As Variant
Dim ticketamount(500) As Variant
Dim paymethod(500) As Variant

Open "C:\Users\Dylaan\Desktop\School Solution\tickets.txt" For Input As #1

Do While Not EOF(1)

Input #1, ticketid(), theatreid(), ticketamount(), paymethod()

lstticketid.AddItem ticketid()
lsttheatreid.AddItem theatreid()
lstticketamount.AddItem ticketamount()
lstmethod.AddItem paymethod()

Exit Do
Loop

Close #1

End Sub

为什么?

3 个答案:

答案 0 :(得分:4)

看看这个:

Private Sub CmdRun_Click()

    Dim ticketid(500) As Variant
    Dim theatreid(500) As Variant
    Dim ticketamount(500) As Variant
    Dim paymethod(500) As Variant

    dim ix as integer

    Open "C:\Users\Dylaan\Desktop\School Solution\tickets.txt" For Input As #1

    ix = 0
    Do While Not EOF(1)

        Input #1, ticketid(ix), theatreid(ix), ticketamount(ix), paymethod(ix)

        lstticketid.AddItem ticketid(ix)
        lsttheatreid.AddItem theatreid(ix)
        lstticketamount.AddItem ticketamount(ix)
        lstmethod.AddItem paymethod(ix)

        ix = ix + 1
    Loop

    Close #1

End Sub

当然你应该考虑使用

freefile (获取文件句柄)

还有 MORE 记录的可能性超出预期......

答案 1 :(得分:0)

Set fso = CreateObject("Scripting.FileSystemObject")
Set srcfile = fso.GetFile("c:\myfile.txt")
If err.number = 0 then Set TS = srcFile.OpenAsTextStream(1, 0)
Src=ts.readall
Arr1=Split(Src, vbcrlf)
For Each thing in Arr1
    Arr2=Split(thing, ",")
    For Each thing2 in Arr2
          msgbox thing2
    Next
Next

这是vbscript但可以在VB6中使用。我们正在使用split命令。

答案 2 :(得分:0)

对您的代码的一些评论:

  • 您不需要将这些数组声明为变体,您可以将它们声明为字符串数组。
  • 由于文件只有500行,您可以立即阅读。
  • 你真正的问题是输入命令:它不能一次读入数组。
  • 同样适用于列表框:您不能一次添加数组。

应用所有内容后,请查看以下测试项目:

'1 form with:
'  1 command button   : name=Command1
'  4 listbox controls : name=List1 name=List2 name=List3 name=List4
Option Explicit

Private Sub Command1_Click()
  Dim strData As String
  strData = ReadFile("c:\temp\file.txt")
  ShowData strData
End Sub

Private Sub Form_Resize()
  Dim sngWidth As Single
  Dim sngCmdHeight As Single
  Dim sngLstWidth As Single, sngLstHeight As Single
  sngWidth = ScaleWidth
  sngCmdHeight = 315
  sngLstHeight = ScaleHeight - sngCmdHeight
  sngLstWidth = sngWidth / 4
  List1.Move 0, 0, sngLstWidth, sngLstHeight
  List2.Move sngLstWidth, 0, sngLstWidth, sngLstHeight
  List3.Move 2 * sngLstWidth, 0, sngLstWidth, sngLstHeight
  List4.Move 3 * sngLstWidth, 0, sngLstWidth, sngLstHeight
  Command1.Move 0, sngLstHeight, sngWidth, sngCmdHeight
End Sub

Private Function ReadFile(strFile As String) As String
  Dim intFile As Integer
  Dim strData As String
  intFile = FreeFile
  Open strFile For Input As #intFile
    strData = Input(LOF(intFile), #intFile)
  Close #intFile
  ReadFile = strData
End Function

Private Sub ShowData(strData As String)
  Dim lngLine As Long
  Dim strLine() As String
  Dim strPart() As String
  strLine = Split(strData, vbCrLf)
  For lngLine = 0 To UBound(strLine)
    strPart = Split(strLine(lngLine), ",")
    If UBound(strPart) = 3 Then
      List1.AddItem strPart(0)
      List2.AddItem strPart(1)
      List3.AddItem strPart(2)
      List4.AddItem strPart(3)
    Else
      'not the correct number of items
    End If
  Next lngLine
End Sub

当您单击Command1时,它将从c:\ temp \ file.txt读取文本文件

之后它将分割数据以形成一个行数组,遍历所有行,将每一行拆分为一部分,并在列表框中显示部分,如果一行上恰好有4个部分。