读取文本文件并将其内容分配给vb 6中的不同变量

时间:2014-10-19 23:36:41

标签: text vb6

我目前正在尝试制作一款RPG游戏。我有保存游戏功能如何工作,但我无法弄清楚如何使加载功能工作。我正在尝试将文本文件内容分配给不同的变量但是当我查找如何时,我总是发现当我尝试将其分配给6个不同的变量时,人们正在将文本文件的内容设置为单个变量。有没有办法让我这样做,或者我是否必须将它们存储到不同的文本文件中?

nFileNum = FreeFile

' Open a text file for input. inputbox returns the path to read the file
Open "C:\DaggerFall\CharacterInformation.txt" For Input As nFileNum

lLineCount = 1

' Read the contents of the file
Do While Not EOF(nFileNum)
    Line Input #nFileNum, sNextLine
    sNextLine = sNextLine & vbCrLf
    sText = sText & sNextLine
Loop

Text1.Text = sText

' Close the file
Close nFileNum

2 个答案:

答案 0 :(得分:0)

您正确读取数据,现在必须根据行上的不同值分割数据

例如我创建了以下文件

a 16 16 10 4 4
b 4 8 6 16 16
c 4 10 16 6 14

此文件由每个字符行组成,每行包含6列:name,str,con,dex,wis,int stats由统计信息之间的空格分隔

我创建了一个测试项目,该项目读入文件,存储数据并在列表框中显示名称 当您单击列表框中的名称时,属于该名称的统计信息将显示在消息框中

'1 form with:
'  1 command button: name=Command1
'  1 listbox control: name=List1
Option Explicit

'the type containing all stats
Private Type CharInfo
  strName As String
  intStr As Integer
  intCon As Integer
  intDex As Integer
  intWis As Integer
  intInt As Integer
End Type

'form level variable to store all the data
Private mudtChar() As CharInfo

Private Sub Command1_Click()
  Dim intLine As Integer, intCount As Integer
  Dim intFile As Integer
  Dim strFile As String
  Dim strData As String
  Dim strLine() As String, strPart() As String
  'clear the listbox
  List1.Clear
  'read the whole file into 1 string variable
  strFile = "c:\temp\file.txt"
  intFile = FreeFile
  Open strFile For Input As #intFile
    strData = Input(LOF(intFile), intFile)
  Close #intFile
  'split the data on strings per line
  strLine = Split(strData, vbCrLf)
  'count the lines to find out how many character names there are in the file
  intCount = UBound(strLine)
  'resize the storage to contain all the data
  ReDim mudtChar(intCount) As CharInfo
  'loop through all line to write the character data into the storage variable
  For intLine = 0 To intCount
    strPart = Split(strLine(intLine), " ")
    With mudtChar(intLine)
      .strName = strPart(0)
      List1.AddItem .strName 'show the name in the listbox
      .intStr = Val(strPart(1))
      .intCon = Val(strPart(2))
      .intDex = Val(strPart(3))
      .intWis = Val(strPart(4))
      .intInt = Val(strPart(5))
    End With 'mudtChar(intLine)
  Next intLine
End Sub

Private Sub List1_Click()
  Dim intIndex As Integer
  Dim strShow As String
  'find the index which was clicked
  intIndex = List1.ListIndex
  With mudtChar(intIndex)
    'build the data which you want to show
    strShow = .strName
    strShow = strShow & vbCrLf & "Str : " & CStr(.intStr)
    strShow = strShow & vbCrLf & "Con : " & CStr(.intCon)
    strShow = strShow & vbCrLf & "Dex : " & CStr(.intDex)
    strShow = strShow & vbCrLf & "Wis : " & CStr(.intWis)
    strShow = strShow & vbCrLf & "Int : " & CStr(.intInt)
    'show the data
    MsgBox strShow, vbInformation, .strName
  End With 'mudtChar(intIndex)
End Sub

我希望这能解释如何从文件中分割数据

答案 1 :(得分:-1)

您正在使用旧文件功能。新的更容易。

这是vbscript代码,可以粘贴到VB6中。但是它的后期绑定(使用set x = createobject("objectname")代替set x = new objectname)和无类型(不as string)。出于速度原因,您希望尽早使用(添加对Microsoft Scripting Runtime)绑定的引用并输入变量。

'Create the object
Set fso = CreateObject("Scripting.FileSystemObject")

'Turn on programmer error handler
On Error Resume Next

'Open a file - 1 means for reading, true means create if it doesn't exist, 0 means ANSI
Set myfile = fso.OpenTextFile("C:\windows\win.ini", 1, true 0)

'test for errors
If err.num <> 0 then
    'Check here for specific problems
    If err.num = 53 then
        'Decide what to do if user deleted file
        err.clear
    Else
        'Decide to do an err.raise and crash.
        err.Raise(err.num, "FSO Object")
        Exit (Sub or Function - whichever)
    End If
End If

'Read the file all at once. `Read` does bytes and `ReadLine` does lines.
FileContents = myfile.ReadAll

'Turn on vbscript error handling    
On Error Goto 0

'Print it
msgbox FileContents

'If you need to close it (it will close as soon as you hit an `End Sub` automatically).
Set myfile = Nothing

'If you need to close the fso object - you are finished with all file operations (it will close as soon as you hit an `End Sub` automatically).
Set fso = Nothing

在字符串中,您可以遍历(Instr),使用Split将其转换为数组。或许多其他事情。