Question Seperating parts of a txt file

SethBartlett

Member
Joined
Dec 3, 2008
Messages
6
Programming Experience
Beginner
I'm building a program that currently reads everything in a text file and outputs it to a textbox. Right now it uses StreamReader and does a ReadToEnd() and places that all in a string EntireFile. EntireFile outputs the data into a text file. So right now I basically made a basic text editor. What I need to do is figure out a way to seperate data and put it into different variables. I can deal with the data once it is placed in seperate variables, I just can't figure out a way to seperate the string. Here is an example file of what I'm trying to seperate.
VB.NET:
#AREA	Name New Area~


#MOBILES
#9900
MobsName~
MobDescrip~
LongDescription
~
the mob looks back at you!

~
64 0 0 S
75 0 0 0d0+0 0d0+0
0 0
8 8 1
#0


#ROOMS
#9900
RoomName~
Description Of Room
~
99 0 1
S
#0


#$

So what I need to seperate are the vnums(The number after #) for each of them, also I need to seperate things that have a ~ after them. Basically each line would be a seperate variable to edit. So RoomName~ would be one variable(~ would be a delimeter). 99, 0 and 1 would be seperate. I just need to figure out how to seperate all these. Either when the program is reading the file or just reading it from the string EntireFile.

Also, I'd like to add that the extension of the file is technically .are (area file) but it can be read perfectly fine by a text file editor and is all text.
Thank You.
 
Last edited:
VB.NET:
Dim lines() as String = File.ReadAllLines(path)


For Each line In lines
  If line.StartsWith("#AREA") Then ...

Good luck!
 
Awesome! Thank you. But how would I do the different variables? Example: 64 0 0 S each one of them is a seperate "bit" to set. Also the 0's can be like 1|2|512, and it would actually look like that in the area file.

I have figured some out, I have pulled out the part after #AREA to get the areaname and stuck it into a variable, I can change this variable and everything, but I would need a way to write it all back into the file correctly.
VB.NET:
For Each line In TestingFile
                        If InStr(line, "#AREA") = 1 Then
                            MsgBox("#AREA is correct!")
                            AreaName = line.Substring(5 + 1)
                            TextBox.Text = line
                            FoundArea = True
                        ElseIf InStr(line, "#AREA") <> 1 And FoundArea <> True Then
                            MsgBox("#AREA is not existant!")
                            sMyFile = ""
                            Me.Text = "Area Builder"
                            FileLocation = sMyFile
                            Exit For
                        ElseIf line.StartsWith("#$") Then
                            MsgBox("End of file!")
                            TextBox.Text += Environment.NewLine
                            TextBox.Text += line
                        Else
                            'MsgBox("Somewhere in the file!")
                            TextBox.Text += Environment.NewLine
                            TextBox.Text += line
                        End If
                    Next

If I changed the variable, I would need it to stay back in the same place as it was before when I save the file, how would I do this?
 
Last edited:
Ugh, Please use line.StartsWith("#AREA") rather than InStr(line, "#AREA") = 1

Avoid:
InStr, Mid, Left, Right, MsgBox

Use:
string.Contains, string.SubString, string.Remove, string.Substring(length - x), MessageBox.Show


Use StringBuilder instead of string concatenation


You might experience better luck using Regular Expressions, but learning them to parse something like this is not a light undertaking.

If I changed the variable, I would need it to stay back in the same place as it was before when I save the file, how would I do this?
You wouldnt. You'd make a class or set of classes that define the file. Parse the file into the classes (shame it's not XML, would have been easier then) and change stuff, then write ToString methods and call them to write them back to a file


VB.NET:
Class Area

  Private areaName as String

  Private thingsHere as New List(Of AreaThings) 


  Sub New(definingLines)
    For Each line as Stirng in definingLines 'the file

    if line.StartsWith("#ROOMS") Then
      'find the end of the rooms block
      thingsHere.Add(New RoomsThing(<only the lines for the rooms block>))
    
      'blah blah 

   End Sub

  Overrides Function ToString() As String

    Dim sb as New StringBuilder

    sb.AppendLine(areaName)

    For Each at as AreaThing in thingsHere
      sb.AppendLine(at.ToString()) 'calls overriden tostring
    Next at

    Return sb.ToString()
  End Function

End Class

MustInherit Class AreaThing

  Protected thingType as String '#MOBILES or #ROOMS or...

  Protected nnzz as String = "#9900"

  Protected nameTildeThing as String


  Overrides Function ToString() As String
    Dim sb as New StringBuilder()

    sb.AppendLine(thingType)
    sb.AppendLine(nnzz)
    sb.AppendLine(nameTildeThing)
    
    Return sb.ToString()

  End Function
End Class

Class MobilesThing Inherits AreaThing

  Sub New(definingLines() as String) 'constructor

    MyBase.thingType = "#MOBILES"

    MyBase.NameTildeThing = LookForTildeLineInDefinition(definingLines)

   'blah blah blah

  End Sub
End Class

This is why Object Orientation is cool. You define a parent class for MOBILES, ROOMS etc and you write a ToString for it, but you declare it MustInherit which means you CANNOT make a New AreaThing.. You must make a New MobilesThing. To do that the MobilesThing constructor can fully parse everything about a ROOMS from a blok of lines that totally defines the ROOMS. It sets all the variables etc, and then you can change them (Make public properties out of them then assign values to the properties)

Then to create the file, you just call area.ToString, which goes through its list of areathings and toStrings all those. Because the children of areathing only ever assign to variables in the parent, it can build the whole thing without you writing a tostring in every child. However if there are specialised bits in the children, you would then overrides tostring again in the children, call MyBase.ToString() from the child to get the common stuff from the parent, then add the specific stuff in the child


If youre new to OO this will probably confuse the heck out of you, but you'll have to learn it if you want to program in an OO fashion. See some more indepth tutorials about polymorphism
 
Woo, that is alot to digest, I'll look into polymorphism, I have been looking for information about it and don't seem to get too much GOOD information. A decent amount point to old information or non-existant/dead links.

Thank you.:)
 
if im wanting to make multiple instances of a class, should I use an array and how large can arrays be? Usually area files have around 100 rooms and maybe up to 500 tops, which would be a huge area file, but would I use an array to store all of the rooms?
 
if im wanting to make multiple instances of a class, should I use an array
No, use a collection like I did: List(Of Rooms)

and how large can arrays be?
How much memory do you have?

Usually area files have around 100 rooms and maybe up to 500 tops, which would be a huge area file, but would I use an array to store all of the rooms?

Your definition of huge, and my definition of huge seem to be different. When I say something like freedb is huge, it's because I'm having trouble loading all 2.6 million discs into one SQLServer Express database and keeping within the 4 Gigabyte database file size limit
 
Hahaha sorry about the "huge" reference. That isn't exactly "huge" to me, I just mean that in the sense of my area files, it is huge. Average area file is around 100 rooms so 500 would be considered huge in an area file. I have a good amount of memory to support a large array but I didn't know about the List of. Thanks for your help once again, I'm still a little stumped, I've figured out how I could write all the data into the area file, which is actually fairly simple cause I'm able to put in the spaces and everything, but reading it is a little more difficult because the lengths aren't always the same in between variables. A setting can be like 8|32 or it could be 4|8|128|512 and such.

Thank you for your help!:)
 
why would you need lengths to be the same? Just read the line, look for the first index of something (using string.IndexOf() ) go forward or back a few characters, take a Substring(), and then Split() it based on the "|"c pipe character ?!
 
it wouldn't get split on the |'s, they are all part of one main thing. In the code that actually reads them into the game, it adds them all together to make one number to read from.
 

Latest posts

Back
Top