【问题标题】:Filling an array from a .txt file VB 6.0从 .txt 文件 VB 6.0 填充数组
【发布时间】:2014-11-27 18:55:09
【问题描述】:

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

我正在尝试从一个文件中填充 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

为什么?

【问题讨论】:

    标签: arrays loops vb6 listbox eof


    【解决方案1】:

    看看这个:

    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 记录超出预期...

    【讨论】:

      【解决方案2】:
      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 命令。

      【讨论】:

        【解决方案3】:

        您的代码中的一些 cmets:

        • 您无需将这些数组声明为变体,您可以将它们声明为字符串数组。
        • 由于文件只有 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 个部分。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-01-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-05
          相关资源
          最近更新 更多