Text 在VB6中读取文本文件并将其内容分配给不同的变量

Text 在VB6中读取文本文件并将其内容分配给不同的变量,text,vb6,Text,Vb6,我目前正在尝试制作一个RPG游戏。我知道save game函数是如何工作的,但我不知道如何使load函数工作。我试图将文本文件内容分配给不同的变量,但当我查看如何分配时,我总是发现当我试图将文本文件内容分配给6个不同的变量时,人们正在将文本文件内容设置为单个变量。我有没有办法做到这一点,或者我必须将它们存储到不同的文本文件中 nFileNum = FreeFile ' Open a text file for input. inputbox returns the path to read t

我目前正在尝试制作一个RPG游戏。我知道save game函数是如何工作的,但我不知道如何使load函数工作。我试图将文本文件内容分配给不同的变量,但当我查看如何分配时,我总是发现当我试图将文本文件内容分配给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

您正在使用旧的文件函数。新的要容易得多

这是vbscript代码,可粘贴到VB6中。但是,它是后期绑定的(使用
set x=createobject(“objectname”)
而不是
set x=newobjectname
)和非类型化的(没有
作为字符串
)。出于速度原因,您希望尽早使用(添加对
Microsoft脚本运行时的引用)绑定并键入变量

'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
“创建对象
设置fso=CreateObject(“Scripting.FileSystemObject”)
'打开程序员错误处理程序
出错时继续下一步
'打开文件-1表示读取,true表示创建(如果不存在),0表示ANSI
设置myfile=fso.OpenTextFile(“C:\windows\win.ini”,1,真0)
"错误测试",
如果err.num为0,则
'查看此处以了解具体问题
如果err.num=53,则
'决定如果用户删除了文件该怎么办
清楚
其他的
'决定做一个错误。升起并坠毁。
err.Raise(err.num,“FSO对象”)
退出(子功能或功能-以两者为准)
如果结束
如果结束
'一次读取所有文件`Read`does字节,`ReadLine` does行。
FileContents=myfile.ReadAll
'启用vbscript错误处理
错误转到0
“打印出来
msgbox文件内容
'如果您需要关闭它(当您自动点击'End Sub'时,它将立即关闭)。
设置myfile=Nothing
'如果您需要关闭fso对象-您完成了所有文件操作(只要您自动点击'End Sub',它就会关闭)。
设置fso=无

在字符串中循环(
Instr
)后,使用
Split
将其转换为数组。或者其他许多事情。

如果正确地读入了数据,现在必须根据一行中的不同值拆分数据

例如,我创建了以下文件

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

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

您已经标记了这个问题
vb.net
,但是您的标题是“vb6”。是哪一个?另外,您实际尝试了什么?它是vb6,我已经尝试了triednFileNum=FreeFile'打开一个文本文件进行输入。inputbox返回读取打开的文件“C:\DaggerFall\CharacterInformation.txt”的路径,输入为nFileNum lLineCount=1“读取文件内容而不执行EOF(nFileNum)行输入#nFileNum,sNextLine sNextLine=sNextLine&vbCrLf sText=sText&sNextLine Loop Text1.Text=sText'关闭文件关闭nFileNumI我已将您的代码复制到您的问题中,并对其进行了格式化,使其可读。我不使用VB6,所以这是我的出发点。您正在读取文件中的所有数据,但您希望将数据的哪些部分指定给哪些变量?您的代码还可以,现在您必须考虑您需要的数据的哪些部分,以及这些部分是如何分离的。。请发布一个数据示例及其构建方式。如您所述,为什么使用FileSystemObject比使用旧文件函数更容易?请尝试帮助介绍文件系统对象模型Visual Basic的一个新功能是文件系统对象(FSO)对象模型,它提供了一个用于处理文件夹和文件的基于对象的工具。这允许您使用熟悉的object.method语法和一组丰富的属性、方法和事件来处理文件夹和文件,以及使用传统的Visual Basic语句和命令。这并不意味着它比旧的标准方法更容易