Jump to content

Load / save data from text file

- - - - -

  • Please log in to reply
20 replies to this topic

#1
wazofski

wazofski

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
I want to make a highscoreboard for my game, but how can I save or load data from a text file, so the highscorerecords will be saved?

#2
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Are you using .NET?
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#3
wazofski

wazofski

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
Yes I am using vb 2005 express edition

#4
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Then it's really easy!

To read all the data from a text file:

Try

 Dim filePath As String = "C:\temp\thefile.txt"
 Dim sr As New System.IO.StreamReader(filePath)
 Dim theText As String = sr.ReadToEnd()

 sr.Close()
 sr.Dispose()

 'At this point, the string variable theText contains the text from the text file.

Catch exc As Exception
 MessageBox.Show("An error has occured: " & exc.Message)
End Try

Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#5
wazofski

wazofski

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
And how would I make a scoreboard of that? Let's say: Tom scored 100 points, How would I do that so when you restart the programm: On 1 label will be the name displayed and on the other laber the score

#6
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Ah. In this case a text file might not be enough. You see, you need to store the data in the text file in such a way that the program can easily read it back into the program again.

For example, you could store them like this:

Tom,100
Bill,200
Charlie,300

As you can see, each new line is a new player, and the values are separated by commas. We could parse out the data, but there is a problem - how would we arrange them on order of points?

Maybe an XML file would be more useful. If we stored it like this:
<game>
 <highscores>
  <score name="Tom" points="100" />
  <score name="Bill" points="200" />
  <score name="Charlie" points="300" />
 </highscores>
</game>

You see? If we do it like this, I can show you how to use the System.XML namespace to automatically read in the data and extract the rows, just like a normal database.

However, you would still need to sort them in order of points. It's your choice!
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#7
wazofski

wazofski

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
Ok I want to do that, how can I add this XML file to it, you really got to help me with this because I have never worked with files actually

#8
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
That's OK - it's a big task to master working with files. However, it just got a weeny bit easier.

You see, the .NET Framework provides a namespace called System.Xml. It allows you to read XML files without having to actually examine every single character and work out whether it is a tag or not etc.

Are you familiar with XML? If not, see my example. It is a way of storing data. The best bit is, it is still just a text file, so you can manipulate it just like a normal text file.

First, we will work out how to read text files - writing them is more complex. Therefore, create a new text file - call it game.xml or something. Write the code for the XML file I gave you into it, so that the game.xml file contains the text:

<game>
 <highscores>
  <score name="Tom" points="100" />
  <score name="Bill" points="200" />
  <score name="Charlie" points="300" />
 </highscores>
</game>

OK. Now go back to your program. I don't know where you want to load the data, but maybe when the window is loaded? Anyway, make the following method (and pardon me if I make a mistake, I'm used to C# not VB):

Private Sub LoadHighScores()

'This is the XML file.
Dim filePath As String = "C:\temp\game.xml"
Dim sr As New StreamReader(filePath)
Dim rawXml As String = sr.ReadToEnd()
sr.Close
sr.Dispose

'Here, rawXml contains the text from the file.

End Sub

OK. After this I need to start explaining the code, so I'm going to write it here. Type all the following code after the comment (just before End Sub):

First, we create a new XmlDocument, that will hold the file:
Dim doc As New System.Xml.XmlDocument()
doc.LoadXml(rawXml)

Now we have loaded the data, we can use it. Here's an example:

For Each node As System.Xml.XmlNode In doc.SelectNodes("game/highscores")
 MessageBox.Show("The player called " & node.Attributes("name").Value & " got the score " & node.Attributes("points").Value & ".")
Next

I think this is what you do in VB, but if you get any errors, let me know. :)
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#9
sakishrist

sakishrist

    Programmer

  • Members
  • PipPipPipPip
  • 109 posts
You solved some of the problems I had with xml. Thanks Xav
Posted via CodeCall Mobile

#10
wazofski

wazofski

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
It says: Type 'StreamReader' Is not defined

#11
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Sorry. It is part of the System.IO namespace. You can do one of two things:

@ Go to the Project Properties page, find the References bit and tick the System.Xml thingy. Or, if you can't be bothered:
@ Just replace StreamReader with System.IO.StreamReader. :)
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#12
wazofski

wazofski

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
It doesn't work for me, but actually what I ment is that you got a label on the form where the name will be displayed and a label with how many points a person has, and that you can save your score.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users