Results 1 to 12 of 12

Thread: Searching in Files..

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2001
    Location
    Chicago, IL
    Posts
    49

    Searching in Files..

    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

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Use INI Files , unlikely i don't how to code that

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  4. #4

    Thread Starter
    Member
    Join Date
    Dec 2001
    Location
    Chicago, IL
    Posts
    49
    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.

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    ok , in such case I strongly suggest to use db file (.MDB)
    If I were you , without a doubt I'd use it .
    cheers

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  7. #7

    Thread Starter
    Member
    Join Date
    Dec 2001
    Location
    Chicago, IL
    Posts
    49
    Im going to go out on a limb and show you my terrible code...

    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

  8. #8

    Thread Starter
    Member
    Join Date
    Dec 2001
    Location
    Chicago, IL
    Posts
    49
    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

  9. #9
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Can you post a sample of the file?

  10. #10

    Thread Starter
    Member
    Join Date
    Dec 2001
    Location
    Chicago, IL
    Posts
    49
    Code:
    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

  11. #11
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim dh As New DataHolder(2, "data.txt")
    3.         MsgBox(dh.Stat(DataHolder.Stats.AC))
    4.     End Sub
    5. End Class
    6.  
    7. Public Class DataHolder
    8.     'enum the stats to save time later
    9.     Public Enum Stats
    10.         Resilience
    11.         Might
    12.         Alertness
    13.         Insight
    14.         AC
    15.         Hits
    16.         Atk
    17.         Level
    18.     End Enum
    19.  
    20.     Private _MobileName As String
    21.     'keep stats in array to save time later
    22.     Private _Stat(7) As String
    23.  
    24.     Public Property MobileName() As String
    25.         Get
    26.             Return _MobileName
    27.         End Get
    28.         Set(ByVal Value As String)
    29.             _MobileName = Value
    30.         End Set
    31.     End Property
    32.  
    33.     Public Property Stat(ByVal item As Stats) As String
    34.         Get
    35.             Return _Stat(item)
    36.         End Get
    37.         Set(ByVal Value As String)
    38.             _Stat(item) = Value
    39.         End Set
    40.     End Property
    41.  
    42.     'pass in id number and filepath
    43.     Public Sub Load(ByVal recNum As Integer, ByVal filename As String)
    44.         'read the file to string
    45.         Dim fs As New FileStream(filename, IO.FileMode.Open)
    46.         Dim sr As New StreamReader(fs)
    47.         Dim data As String = sr.ReadToEnd()
    48.         sr.Close()
    49.  
    50.         'find the id we want
    51.         Dim istart As Integer = data.IndexOf(String.Concat(recNum, ":"))
    52.         'read the name
    53.         Dim ln As String = getLine(istart, data)
    54.         MobileName = ln.Substring(2) 'trim id off
    55.         Dim cnt As Integer
    56.         'get proceeding lines for stats one at a time
    57.         For cnt = 0 To 7
    58.             istart = istart + ln.Length + 2 'increments position and adds 2 for the newline character
    59.             ln = getLine(istart, data)
    60.             _Stat(cnt) = ln
    61.         Next
    62.     End Sub
    63.  
    64.     'gets a complete line
    65.     Private Function getLine(ByVal start As Integer, ByVal data As String) As String
    66.         Dim istop As Integer = data.IndexOf(ControlChars.NewLine, start)
    67.         Return data.Substring(start, istop - start)
    68.     End Function
    69.  
    70.     Public Sub New(ByVal recNum As Integer, ByVal filename As String)
    71.         Me.Load(recNum, filename)
    72.     End Sub
    73.  
    74. End Class
    Last edited by Edneeis; Nov 12th, 2002 at 05:41 PM.

  12. #12

    Thread Starter
    Member
    Join Date
    Dec 2001
    Location
    Chicago, IL
    Posts
    49
    Perfect thanks so much! Thats a nice class too, wow, thanks again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width