Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Writing And Reading Specific Lines Of Text From A Text File


  • Please log in to reply
14 replies to this topic

#1 PlyPencil

PlyPencil

  • Members
  • 51 posts
  • OFFLINE
  •  
  • Local time:03:42 PM

Posted 26 September 2008 - 03:35 PM

Hello, I am PlyPencil, I am not a beginner to VB but I have a lot to learn.

Basically I need a model I can edit and change for a game I am making. I have the following text

[Sector=Universe]
[0,0]
TileName = "SpaceFULL"
[End]
[End]

I have the variables TileType, SectorName, COx and COy.

I need a code which will find the text "[Sector=" + sectorname.tostring in the file and will record the line number in a variable. It will then continue until it finds the [End] (which applies to the Sector) and will record the line number in another variable.

Then it will take the start line number and search for "["cox + "," + coy + "]" ([0,0]) until the end line number, if the text is found is will record the start line and end line in the variables. It will now goto the TileName line and the variable TileType equels the text within the speech marks. If [0,0] was not found it will set TileType as "EnergyFULL"

I hope I have made myself clear in what I want.
Thanks for any help
Ply.

Edited by PlyPencil, 26 September 2008 - 03:47 PM.


BC AdBot (Login to Remove)

 


#2 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:07:42 AM

Posted 26 September 2008 - 03:49 PM

You could use XML and the W3C DOM.

http://www.informit.com/guides/content.asp...l&seqNum=17

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#3 PlyPencil

PlyPencil
  • Topic Starter

  • Members
  • 51 posts
  • OFFLINE
  •  
  • Local time:03:42 PM

Posted 26 September 2008 - 03:55 PM

Thank you for the link, from what I have read it will serve the purpose a lot easier tahn what I planned to do. I will post here again if I run into any problems.

#4 PlyPencil

PlyPencil
  • Topic Starter

  • Members
  • 51 posts
  • OFFLINE
  •  
  • Local time:03:42 PM

Posted 30 September 2008 - 12:03 PM

I can now write XML however I cannot read it. May I have an example from someone?

#5 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:08:42 AM

Posted 30 September 2008 - 12:27 PM

xml is just text....so you are pretty much back right where you started.

Did you happen to see this thread?

#6 PlyPencil

PlyPencil
  • Topic Starter

  • Members
  • 51 posts
  • OFFLINE
  •  
  • Local time:03:42 PM

Posted 30 September 2008 - 01:20 PM

Thank you, I now have all the knowledge I need for my program. For now that is lol.

#7 PlyPencil

PlyPencil
  • Topic Starter

  • Members
  • 51 posts
  • OFFLINE
  •  
  • Local time:03:42 PM

Posted 30 September 2008 - 01:55 PM

Here's my code

Public Class Form1


	Dim texttowrite As String = ""
	Dim cox As Integer = 1
	Dim coy As Integer = 1
	Dim sectorname As String = "Universe"
	Dim tiletype As String
	Dim value As Integer
	Dim holderA As String
	Dim lineNO As Integer
	Dim lineSTART As Integer
	Dim lineEND As Integer

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim tw As New IO.StreamWriter("C:\Users\Ashley\Documents\MultiText4.txt")
		tw.WriteLine("[Sector=" + sectorname + "]")
1:
		Randomize()
		value = CInt(Int((2 * Rnd()) + 1))
		If value = 1 Then
			tiletype = "water"
		End If
		If value = 2 Then
			tiletype = "dirt"
		End If

		tw.WriteLine("[" + cox.ToString + "," + coy.ToString + "]")
		tw.WriteLine("Tile Type = " + tiletype)
		tw.WriteLine("[END=" + cox.ToString + "," + coy.ToString + "]")
		tw.WriteLine()

		cox += 1
		If cox = 21 Then
			coy += 1
			cox = 1
		End If
		If coy = 21 Then
			cox = 1
			coy = 1
			GoTo 2
		End If
		GoTo 1
2:
		tw.WriteLine("")
		tw.WriteLine("[END=" + sectorname + "]")
		tw.Close()
		MsgBox("Finished")
	End Sub

	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		Dim tr As New IO.StreamReader("C:\Users\Ashley\Documents\MultiText4.txt")
		Me.Visible = False
1:
		Me.Text = (tr.ReadLine())
		TextBox1.Text += Me.Text
		TextBox1.Text += Environment.NewLine
		If Me.Text = "[END=Universe]" Then
			GoTo 2
		End If
		Me.Update()
		Me.Refresh()
		GoTo 1
2:
		tr.Close()
		Me.Visible = True
		MsgBox("Done")
	End Sub

	Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
		cox = 16
		coy = 4
		Dim tr As New IO.StreamReader("C:\Users\Ashley\Documents\MultiText4.txt")
		Dim tr2 As New IO.StreamReader("C:\Users\Ashley\Documents\MultiText4.txt")

1:	  Me.Text = "1"
		holderA = tr.ReadLine
		lineNO += 1
		If holderA = "[Sector=Universe]" Then
			lineSTART = lineNO
			GoTo 2
		End If
		GoTo 1

2:	  Me.Text = "2"
		lineNO = 0
		holderA = tr.ReadLine
		lineNO += 1
		If holderA = "[END=Universe]" Then
			lineEND = lineNO
			GoTo 3
		End If
		GoTo 2

3:	  Me.Text = "3"
		lineNO = 0
4:	  Me.Text = "4"
		tr2.ReadLine()
		lineNO += 1
		If lineNO = lineSTART Then
			GoTo 5
		End If
		GoTo 4

5:	  Me.Text = "5"
		lineNO = 0
		holderA = tr2.ReadLine
		lineNO += 1
		If holderA = "[12,4]" Then
			holderA = tr2.ReadLine()
			GoTo 6
		End If
		If holderA = "[END=Universe]" Then
			GoTo 6
			MsgBox("ERROR NOT FOUND")
		End If
		GoTo 5



6:
		Me.Text = "6"
		lineNO = 0
		If holderA = "Tile Type = water" Then
			MsgBox("Water")
		End If
		If holderA = "Tile Type = dirt" Then
			MsgBox("Dirt")
		End If


		MsgBox("done")
		tr.Close()
		tr.Dispose()
		tr2.Close()
		tr2.Dispose()
		lineNO = 0
		holderA = ""
		lineSTART = 0
		lineEND = 0
		MsgBox("")
	End Sub

	Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

	End Sub
End Class

I write to the file (button1), I then ask it to find the data (button 3) the msgbox returns either water or dirt. But if I write for a second time and ask it to find the data it skips the the done msgbox. I do not know why....I have gone over the code but I cannot see a problem with it.

#8 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:08:42 AM

Posted 30 September 2008 - 02:02 PM

Why not print out the contents of holderA right before the check to see what value it is really holding?

#9 PlyPencil

PlyPencil
  • Topic Starter

  • Members
  • 51 posts
  • OFFLINE
  •  
  • Local time:03:42 PM

Posted 30 September 2008 - 02:18 PM

it equals [END=Universe] I think I know what's happened, the line number is not resetting to 0, how would I do that manually?

#10 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:08:42 AM

Posted 30 September 2008 - 02:36 PM

Are you trying to get a specific line of text from a file?

#11 PlyPencil

PlyPencil
  • Topic Starter

  • Members
  • 51 posts
  • OFFLINE
  •  
  • Local time:03:42 PM

Posted 30 September 2008 - 02:46 PM

It cycles from line 1 all the way to the end but the line number does not go to 0 once its finished. I would like to go to a specific line, line 0.

#12 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:08:42 AM

Posted 30 September 2008 - 02:57 PM

It cycles from line 1 all the way to the end but the line number does not go to 0 once its finished.

Of course not. A reader reads the file from start to finish, and then it is done. If you want to read the first line of a text file after reading it, then you have to read it again. That was why I pointed you to that other thread since it provided a solution by which one does not have to keep rereading the file, which is horribly inefficient.

#13 PlyPencil

PlyPencil
  • Topic Starter

  • Members
  • 51 posts
  • OFFLINE
  •  
  • Local time:03:42 PM

Posted 30 September 2008 - 03:04 PM

Do you mean by using arrays? for what I am doing arrays would be more of a resource drain than by reading a text file. Also I have no clue how arrays work.

#14 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:08:42 AM

Posted 30 September 2008 - 03:51 PM

If you have no clue how arrays work, then how can you claim that using an array would be more of a resource drain? Using an array holds the data in memory. Reading a file requires hard drive access. Hard drive access consumes more CPU cycles because the hard drive requires cycles just to run.

If you can't use an array, then you are going to have to re-read the text file again to get the first line of text. There is no other way around it.

#15 PlyPencil

PlyPencil
  • Topic Starter

  • Members
  • 51 posts
  • OFFLINE
  •  
  • Local time:03:42 PM

Posted 10 October 2008 - 01:41 PM

Because the array was constantly being changed, I was under the impression that the array had to be reloaded each time a value changed. I have since been proved wrong and now my game has use of arrays in it.

Thanks for your help
Ply.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users