代码之家  ›  专栏  ›  技术社区  ›  Shadow Moon

如何在VB 6.0中读写逗号分隔的文本值

  •  -1
  • Shadow Moon  · 技术社区  · 7 年前

    -1,0,3,0,5,4,6,7,8,9
    

    Name: [ Value -1 ]
    Nationality: [ Value 0 ]
    Experience: [ Value 3 ]
    

    等等

    因此,当我在程序文本框中输入这些值并点击保存文件时,它会用新值保存文件。就是这么简单。谢谢你们!

    2 回复  |  直到 2 年前
        1
  •  1
  •   user7391836    7 年前

    (注意:这个答案假设文本文件只包含一行。)

    首先,您需要读取文本文件:

    Dim rawData as string
    
    Dim sFileText as String
    Dim FileNo as Integer
    FileNo = FreeFile
    Open "C:\test.txt" For Input As #FileNo 'you should change the file path
    Line Input #FileNo, sFileText 'read the whole line
    rawData = sFileText 'store the first line of the text file in 'rawData'
    Close #FileNo
    

    接下来,需要用逗号分割原始数据:

    Dim data() as string 'an array that will hold each value
    data = Split(rawData, ",") 'split 'rawData' with a comma as delimiter
    

    现在,第一个值存储在数据(0)中,第二个值存储在数据(1)中,以此类推。

    至于“保存文件”按钮,您可以执行以下操作:

    Dim newData as String
    newData = data(0) & "," & data(1) & "," & data(2) 'etc.
    

    然后 write it to a file .

        2
  •  0
  •   ScytheRider    7 年前

    Write #filenumer Value1, Value2, Value3...