System.IO help

Picking out specific values from a text file

Page 1 of 1

3 Replies - 1025 Views - Last Post: 14 June 2009 - 02:16 PM Rate Topic: -----

#1 AndyWhite87   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 01-June 09

System.IO help

Posted 13 June 2009 - 02:32 PM

Hey There,

Basically I'm having trouble. Ive been working on it for ages, trying to figure it out. I have a text file which looks like this (FileName = NPC.dat; FilePath = "C:\Users\Andy\Desktop\NPC.dat"):

[INIT]
NumNPCs=2

[NPC1]
Name=Test NPC
Desc=Hi I'm a test NPC.
Head=1
Body=1
Heading=3
Movement=2
Attackable=0
Hostile=0
Quest=1

[NPC2]
Name=Test NPC 2
Desc=Hi I'm a test NPC.
Head=1
Body=1
Heading=3
Movement=3
RespawnWait=5000
Attackable=1
Hostile=1
GiveEXP=50
GiveGLD=50
MinHP=20
MaxHP=20
MaxHIT=5
MinHIT=1
DEF=5


I want to be able to get, for example, NPC(5).Name = "Lava King" or NPC(1).ATK=4. However, i'll need to be able to access them singularly - ive only managed to be able to ReadLine so having to read the whole file.

This is the VB6 Function, was wondering if u cud point me in the .net direction

Function GetVar(File As String, Main As String, Var As String) As String
'*****************************************************************
'Gets a variable from a text file
'*****************************************************************
	Dim sSpaces As String ' This will hold the input that the program will retrieve
	Dim szReturn As String ' This will be the defaul value if the string is not found
	
	szReturn = ""
	
	sSpaces = Space(5000) ' This tells the computer how long the longest string can be. If you want, you can change the number 75 to any number you wish
	
	getprivateprofilestring Main, Var, szReturn, sSpaces, Len(sSpaces), File
	
	GetVar = RTrim(sSpaces)
	GetVar = Left(GetVar, Len(GetVar) - 1)
End Function


e.g.:

NumNPCs = Val(GetVar(IniPath & "NPC.dat", "INIT", "NumNPCs"))
Which would result in NumNPCs = 2

Hope you can help.

p.s. getprivateprofilestring:
Declare Function getprivateprofilestring Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationname As String, ByVal lpKeyname As Any, ByVal lpdefault As String, ByVal lpreturnedstring As String, ByVal nsize As Long, ByVal lpfilename As String) As Long

This post has been edited by AndyWhite87: 13 June 2009 - 02:33 PM


Is This A Good Question/Topic? 0
  • +

Replies To: System.IO help

#2 tom_b   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 29
  • Joined: 31-December 08

Re: System.IO help

Posted 13 June 2009 - 07:36 PM

One way to do it, would be to create an NPC object, holding values for the npc (exp, name...etc), and read the entire npc.dat file. Then create a new NPC object for each NPC in the .dat, and save to an NPC list.

For example below, start a new project, and add 2 command buttons:

Imports System.IO
Imports System.Text.RegularExpressions

Public Class Form1
	Dim newNPC As NPC
	Dim listNPC As New List(Of NPC)

	Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		Dim strProperties() As String
		Dim strNPCFileLine As String
		'just change path to whatever you use
		Dim objReader As StreamReader = File.OpenText("e:\npc.dat")

		'Keep reading til end of file
		Do Until objReader.EndOfStream

			strNPCFileLine = objReader.ReadLine

			'if line isnt a blank line..
			If strNPCFileLine <> "" Then
				'use Regular Expressions to look for [NPC*] in string ([NPC1, [NPC2], etc..)
				If Regex.Match(strNPCFileLine, "\[NPC.*]").Value.ToString <> "" Then
					'create new NPC object, add to list
					newNPC = New NPC
					listNPC.Add(newNPC)
					'set ID as [NPC*]
					listNPC.Last.ID = Regex.Match(strNPCFileLine, "\[NPC.*]").Value.ToString
				Else
					'line isnt [NPC*], so must be value
					'split value using "=" as deliminator, and save to strProperties array
					strProperties = Split(strNPCFileLine, "=", )
					'add properties to NPC
					NPCAddProperty(listNPC.Last, strProperties(0), strProperties(1))
				End If
			End If

		Loop

		objReader.Close()

	End Sub

	Private Sub NPCAddProperty(ByVal objNPC As NPC, ByVal strProperty As String, ByVal strValue As String)

		Select Case strProperty
			Case "Name"
				objNPC.Name = strValue
			Case "Desc"
				objNPC.Desc = strValue
			Case "Head"
				objNPC.Head = strValue
			Case "Body"
				objNPC.Body = strValue
			Case "Attackable"
				objNPC.Attackable = strValue
			Case "GiveEXP"
				objNPC.GiveEXP = strValue
		End Select

	End Sub

	Public Function FindNPC(ByVal strID As String) As NPC

		'search thru each NPC in list, and return proper NPC
		For Each fNPC As NPC In listNPC
			If fNPC.ID = strID Then
				Return fNPC
				Exit For
			End If
		Next

	End Function

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

		MsgBox("Total Number Of NPCs: " & listNPC.Count)

	End Sub

	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

		MsgBox(FindNPC("[NPC2]").Name)

	End Sub
End Class

Public Class NPC
	Public ID As String = ""
	Public Name As String = ""
	Public Desc As String = ""
	Public Head As String = ""
	Public Body As String = ""
	Public Attackable As String = ""
	Public GiveEXP As String = ""
End Class




For the npc values, copy below and save to npc.dat"

[NPC1]
Name=Tom Jones
Desc=A singer
Head=1
Body=1
Attackable=1
GiveEXP=2

[NPC2]
Name=Rambo
Desc=Behind You
Head=1
Body=3
Attackable=1
GiveEXP=50

[NPC3]
Name=Bunny
Desc=Ribbit
Head=1
Body=1
Attackable=0
GiveEXP=0


In this example I only used a few of your values, but it should give you a general idea of how it works.
Button 1 will show how many NPC's are in the list, and Button 2 will get a specific value from an npc. In this case, the name of "[NPC2]" .

This post has been edited by tom_b: 13 June 2009 - 07:38 PM

Was This Post Helpful? 0
  • +
  • -

#3 AndyWhite87   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 01-June 09

Re: System.IO help

Posted 14 June 2009 - 08:52 AM

Thanks Tom,

I see what you're doing there and think i can replicate it to my means.

This is just a general recommendation, would u recommend storing and retrieving data using SQL, Databases or TextFiles? I only used textfiles as i have no experience with SQL, but could learn. Thanks
Was This Post Helpful? 0
  • +
  • -

#4 tom_b   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 29
  • Joined: 31-December 08

Re: System.IO help

Posted 14 June 2009 - 02:16 PM

I suppose it depends on the type/amount of data your storing. If its nothing too complex, you can just keep it in text files, using your own form of "database" format. Setup a couple of routines that opens and saves your data to your file, in a format thats easy to parse. If you really wanna get full into working with real database's, http://www.homeandle.../NET/vbNet.html has some good tutorials in part 13, in addition to many other tutorials and such there as well.

View PostAndyWhite87, on 14 Jun, 2009 - 07:52 AM, said:

Thanks Tom,

I see what you're doing there and think i can replicate it to my means.

This is just a general recommendation, would u recommend storing and retrieving data using SQL, Databases or TextFiles? I only used textfiles as i have no experience with SQL, but could learn. Thanks

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1