Click to See Complete Forum and Search --> : Searching in Files..
DBridle
Nov 12th, 2002, 02:16 PM
Hi Guys,
I'm stuck again ;(
I have a file structure that looks something like this
1:<name>
<int>
<int>
<int>
<int>
<int>
2:<name>
...
3:<name>
...
4:<name>
The ID before <name> seperated by a ':' is the ID I am searching for.
So I want to search for ID say 10 and get back the next 6 lines into class members.
Any help on this would be great, I did write somehting but well, it sucks and keeps locking up on me :(
- Darren
Pirate
Nov 12th, 2002, 02:24 PM
Use INI Files , unlikely i don't how to code that
Edneeis
Nov 12th, 2002, 02:31 PM
How big are the files? Can you read them into a string?
If so you can just split the string on : and get the id you are looking for.
DBridle
Nov 12th, 2002, 02:38 PM
Well the string manipulation is not the part im struggling with, its hte logic to read the lines and search. Look for 10: read the next 6 lines. Sounds simple, seems harder to code.
Pirate
Nov 12th, 2002, 02:44 PM
ok , in such case I strongly suggest to use db file (.MDB)
If I were you , without a doubt I'd use it .:D
cheers:)
Edneeis
Nov 12th, 2002, 02:49 PM
But if you split on : then the string would be from the character after the id all the way to the next ID which would be the next 6 lines also. If you need to break it up further then you could split on ControlChars.Newline to get the individual lines.
DBridle
Nov 12th, 2002, 03:28 PM
Im going to go out on a limb and show you my terrible code...
Public Function LoadMobileData() As Boolean
Dim fStream As New FileStream("mob.dat", FileMode.Open)
Dim sReader As New StreamReader(fStream)
Dim buffer As String
buffer = sReader.ReadLine
While buffer <> ""
If buffer.Chars(1) = ":" Then
If buffer.Chars(0) = mMobileNumber.ToString Then
mMobileName = Mid(buffer, 3)
mStatResilience = sReader.ReadLine
mStatMight = sReader.ReadLine
mStatAlertness = sReader.ReadLine
mStatInsight = sReader.ReadLine
mStatAC = sReader.ReadLine
mStatHits = sReader.ReadLine
mStatAtk = sReader.ReadLine
mStatLevel = sReader.ReadLine
LoadMobileData = True
MessageBox.Show("Mobile : " & mMobileName & " loaded successfuly.")
Exit Function
Else
sReader.ReadLine()
sReader.ReadLine()
sReader.ReadLine()
sReader.ReadLine()
sReader.ReadLine()
sReader.ReadLine()
sReader.ReadLine()
End If
End If
buffer = sReader.ReadLine
End While
End Function
While it doesnt work, that was not the intention, the intention was to get hte logic right, as you can see, its not good :/
Any help would be great,
Darren
DBridle
Nov 12th, 2002, 03:33 PM
If I was using a business application, then I would use MDB Files, but since its not its for a small game im writing for my son, I want it to be fast, and hidden away :)
Edneeis
Nov 12th, 2002, 03:41 PM
Can you post a sample of the file?
DBridle
Nov 12th, 2002, 03:43 PM
1:Mobile Encounter 1
10
6
7
3
7
20
6
1
2:Mobile Encounter 2
20
12
14
4
10
40
8
2
There you go :)
Edneeis
Nov 12th, 2002, 04:29 PM
This would be much easier if the data was kep as xml or if the class/collection was directly serialized to file, but since its not here is what i got for you:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dh As New DataHolder(2, "data.txt")
MsgBox(dh.Stat(DataHolder.Stats.AC))
End Sub
End Class
Public Class DataHolder
'enum the stats to save time later
Public Enum Stats
Resilience
Might
Alertness
Insight
AC
Hits
Atk
Level
End Enum
Private _MobileName As String
'keep stats in array to save time later
Private _Stat(7) As String
Public Property MobileName() As String
Get
Return _MobileName
End Get
Set(ByVal Value As String)
_MobileName = Value
End Set
End Property
Public Property Stat(ByVal item As Stats) As String
Get
Return _Stat(item)
End Get
Set(ByVal Value As String)
_Stat(item) = Value
End Set
End Property
'pass in id number and filepath
Public Sub Load(ByVal recNum As Integer, ByVal filename As String)
'read the file to string
Dim fs As New FileStream(filename, IO.FileMode.Open)
Dim sr As New StreamReader(fs)
Dim data As String = sr.ReadToEnd()
sr.Close()
'find the id we want
Dim istart As Integer = data.IndexOf(String.Concat(recNum, ":"))
'read the name
Dim ln As String = getLine(istart, data)
MobileName = ln.Substring(2) 'trim id off
Dim cnt As Integer
'get proceeding lines for stats one at a time
For cnt = 0 To 7
istart = istart + ln.Length + 2 'increments position and adds 2 for the newline character
ln = getLine(istart, data)
_Stat(cnt) = ln
Next
End Sub
'gets a complete line
Private Function getLine(ByVal start As Integer, ByVal data As String) As String
Dim istop As Integer = data.IndexOf(ControlChars.NewLine, start)
Return data.Substring(start, istop - start)
End Function
Public Sub New(ByVal recNum As Integer, ByVal filename As String)
Me.Load(recNum, filename)
End Sub
End Class
DBridle
Nov 12th, 2002, 04:37 PM
Perfect thanks so much! Thats a nice class too, wow, thanks again :) :) :)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.