Closed Thread
Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: Load / save data from text file

  1. #1
    wazofski is offline Programmer
    Join Date
    Feb 2008
    Posts
    126
    Rep Power
    0

    Load / save data from text file

    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. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Load / save data from text file

    Are you using .NET?

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  4. #3
    wazofski is offline Programmer
    Join Date
    Feb 2008
    Posts
    126
    Rep Power
    0

    Re: Load / save data from text file

    Yes I am using vb 2005 express edition

  5. #4
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Load / save data from text file

    Then it's really easy!

    To read all the data from a text file:

    Code:
    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

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  6. #5
    wazofski is offline Programmer
    Join Date
    Feb 2008
    Posts
    126
    Rep Power
    0

    Re: Load / save data from text file

    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

  7. #6
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Load / save data from text file

    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:

    Code:
    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:
    Code:
    <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!

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  8. #7
    wazofski is offline Programmer
    Join Date
    Feb 2008
    Posts
    126
    Rep Power
    0

    Re: Load / save data from text file

    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

  9. #8
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Load / save data from text file

    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:

    Code:
    <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):

    Code:
    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:
    Code:
    Dim doc As New System.Xml.XmlDocument()
    doc.LoadXml(rawXml)
    Now we have loaded the data, we can use it. Here's an example:

    Code:
    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.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  10. #9
    sakishrist's Avatar
    sakishrist is offline Programmer
    Join Date
    Dec 2007
    Location
    Greece - Syros
    Posts
    109
    Rep Power
    0
    You solved some of the problems I had with xml. Thanks Xav
    Posted via CodeCall Mobile

  11. #10
    wazofski is offline Programmer
    Join Date
    Feb 2008
    Posts
    126
    Rep Power
    0

    Re: Load / save data from text file

    It says: Type 'StreamReader' Is not defined

Closed Thread
Page 1 of 3 123 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Load a file and display data in a richTextBox
    By Bongkerz in forum C# Programming
    Replies: 0
    Last Post: 12-16-2010, 04:26 AM
  2. how to save data in ms-excel file by using php FORM'S ????
    By hamayun_4u2004 in forum PHP Development
    Replies: 4
    Last Post: 04-27-2010, 01:12 PM
  3. How to save the text in a text file ???
    By kresh7 in forum Visual Basic Programming
    Replies: 1
    Last Post: 04-11-2010, 02:03 AM
  4. How do i Save/Load my own file
    By kavery in forum C# Programming
    Replies: 4
    Last Post: 01-28-2010, 06:37 PM
  5. Save/Load Data?
    By slovig in forum Visual Basic Programming
    Replies: 2
    Last Post: 03-04-2008, 03:07 PM

Tags for this Thread

Bookmarks

Posting Permissions

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