Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. probelm on Reading txt file..??

probelm on Reading txt file..??

Scheduled Pinned Locked Moved Visual Basic
questiontestingbeta-testing
14 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C campbells

    the game maybe have 1-4 player, so let said in this round have 3 players player:1 NAme:A Position:10 Movestep:2 player:2 NAme:B Position:8 Movestep:2 player:3 NAme:C Position:7 Movestep:2 -- so i was thinking to use the x value to loop 3 times since got 3 player and read 4 line accordinly.... am not sure can it work but i only know a bit on using the readline.

    D Offline
    D Offline
    Dave Kreskowiak
    wrote on last edited by
    #5

    This is a really inefficient way of doing this. You'd be better off writing it in a CSV format (commna delimited):

    Name,Age,Sex,Something
    "Amy",17,"F",123456
    "Bobby",18,"M",123456

    This will make it easier to parse and easier to recover from a lightly corrupted file. Better yet, use XML! RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

    C 1 Reply Last reply
    0
    • D Dave Kreskowiak

      This is a really inefficient way of doing this. You'd be better off writing it in a CSV format (commna delimited):

      Name,Age,Sex,Something
      "Amy",17,"F",123456
      "Bobby",18,"M",123456

      This will make it easier to parse and easier to recover from a lightly corrupted file. Better yet, use XML! RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      C Offline
      C Offline
      campbells
      wrote on last edited by
      #6

      ?????? noo... actually thats example only wat i mean is i need to loading those information into some label.text, so that the form can perform some task according to the function.

      D 1 Reply Last reply
      0
      • C campbells

        ?????? noo... actually thats example only wat i mean is i need to loading those information into some label.text, so that the form can perform some task according to the function.

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #7

        And you didn't create this file? It's comming from an outside source that you don't control? If you did, then my suggestion still stands. It in no way change what you want to do with this in relation to the forms controls. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        C 1 Reply Last reply
        0
        • D Dave Kreskowiak

          And you didn't create this file? It's comming from an outside source that you don't control? If you did, then my suggestion still stands. It in no way change what you want to do with this in relation to the forms controls. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

          C Offline
          C Offline
          campbells
          wrote on last edited by
          #8

          ??nonono i had already create the file here how i save it, so now i want to load the information back. ...................... respond = MsgBox("Save game ???", MsgBoxStyle.Question + MsgBoxStyle.YesNo) If respond = MsgBoxResult.Yes Then strmw = OpenStreamWriter(filePath) strmw.Write(vbCrLf) 'loop all the player status For x = 1 To Val(NumPlayer.Text) ' write detail in the record strmw.WriteLine(String.Format("Player:" & (x), Now)) strmw.WriteLine(String.Format(" Name: " & wpname(x).Text, Now)) strmw.WriteLine(String.Format("position:" & wPP(x).Text, Now)) strmw.WriteLine(String.Format(" MoveStep:" & wcounter(x).Text, Now)) savegamesound() Next strmw.WriteLine(String.Format("whosturn: " & PTurn.Text, Now)) strmw.Close() MsgBox("Game Saved", MsgBoxStyle.OKOnly) Else : Exit Sub End If

          D 1 Reply Last reply
          0
          • C campbells

            ??nonono i had already create the file here how i save it, so now i want to load the information back. ...................... respond = MsgBox("Save game ???", MsgBoxStyle.Question + MsgBoxStyle.YesNo) If respond = MsgBoxResult.Yes Then strmw = OpenStreamWriter(filePath) strmw.Write(vbCrLf) 'loop all the player status For x = 1 To Val(NumPlayer.Text) ' write detail in the record strmw.WriteLine(String.Format("Player:" & (x), Now)) strmw.WriteLine(String.Format(" Name: " & wpname(x).Text, Now)) strmw.WriteLine(String.Format("position:" & wPP(x).Text, Now)) strmw.WriteLine(String.Format(" MoveStep:" & wcounter(x).Text, Now)) savegamesound() Next strmw.WriteLine(String.Format("whosturn: " & PTurn.Text, Now)) strmw.Close() MsgBox("Game Saved", MsgBoxStyle.OKOnly) Else : Exit Sub End If

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #9

            Nonononono!!! Since you control writing the file, you're doing it in such a way that makes it easy to write but also makes it very tedious to read it back in. What's up with these Now's?? Since your Format's don't have any parameters, calling Now all the time isn't doing anythis. And using String.Format doesn't benefit you either!

            strmw.WriteLine(String.Format("Player:" & x, Now)) '?????????

            If I was in your position, I'd be saving all my Player data into a collection of Player objects. Then you can just serialize the collection out to an XML file and read it back in just as easy.

            Public Class Player
            Private _PlayerName As String
            Private _Position As Integer '? I don't know what data type you're using
            Private _MoveStep As Integer '? I don't know what data type you're using
             
            Public Property PlayerName As String
            Get
            Return _PlayerName
            Eng Get
            Set(ByVal value As String)
            _PlayerName = value
            End Set
            End Property
             
            .
            . ' Same thing for your other Player properties
            .
             
            Public Sub New()
            End Sub
             
            Public Sub New(ByVal playerName As String, ByVal position As Integer, ByVal moveStep As Integer)
            Me.PlayerName = playerName
            Me.Position = position
            Me.MoveStep = moveStep
            End Sub

            Then, to maintain a collection of Players, in an appropriate place in your code:

            Dim Players() As Player

            (This is quik and dirty mind you! I'd actually create another class to maintain the Players collection and put the SaveData and LoadData methods in this class). Now, to save your Player data, all you need to do is something like this:

            Public Sub SavePlayerData()
            Dim sw As New StreamWriter(Path.Combine(Application.StartupPath, "PlayerData.xml"))
            Dim xmls As New XmlSerializer(Players.GetType())
            xmls.Serialize(sw, Players)
            sw.Close()
            End Sub

            To load that data:

            Public Function LoadPlayerData()
            Dim sr As New StreamReader(Path.Combine(Application.StartupPath, "PlayerData.xml"))
            Dim xmls As New XmlSerializer(Players.GetType())
            Players = CType(xmls.Deserialize(sr), Player())

            Done! You don't have to do any parsing of lines or data because the Serializer does it all for you! RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

            C 1 Reply Last reply
            0
            • D Dave Kreskowiak

              Nonononono!!! Since you control writing the file, you're doing it in such a way that makes it easy to write but also makes it very tedious to read it back in. What's up with these Now's?? Since your Format's don't have any parameters, calling Now all the time isn't doing anythis. And using String.Format doesn't benefit you either!

              strmw.WriteLine(String.Format("Player:" & x, Now)) '?????????

              If I was in your position, I'd be saving all my Player data into a collection of Player objects. Then you can just serialize the collection out to an XML file and read it back in just as easy.

              Public Class Player
              Private _PlayerName As String
              Private _Position As Integer '? I don't know what data type you're using
              Private _MoveStep As Integer '? I don't know what data type you're using
               
              Public Property PlayerName As String
              Get
              Return _PlayerName
              Eng Get
              Set(ByVal value As String)
              _PlayerName = value
              End Set
              End Property
               
              .
              . ' Same thing for your other Player properties
              .
               
              Public Sub New()
              End Sub
               
              Public Sub New(ByVal playerName As String, ByVal position As Integer, ByVal moveStep As Integer)
              Me.PlayerName = playerName
              Me.Position = position
              Me.MoveStep = moveStep
              End Sub

              Then, to maintain a collection of Players, in an appropriate place in your code:

              Dim Players() As Player

              (This is quik and dirty mind you! I'd actually create another class to maintain the Players collection and put the SaveData and LoadData methods in this class). Now, to save your Player data, all you need to do is something like this:

              Public Sub SavePlayerData()
              Dim sw As New StreamWriter(Path.Combine(Application.StartupPath, "PlayerData.xml"))
              Dim xmls As New XmlSerializer(Players.GetType())
              xmls.Serialize(sw, Players)
              sw.Close()
              End Sub

              To load that data:

              Public Function LoadPlayerData()
              Dim sr As New StreamReader(Path.Combine(Application.StartupPath, "PlayerData.xml"))
              Dim xmls As New XmlSerializer(Players.GetType())
              Players = CType(xmls.Deserialize(sr), Player())

              Done! You don't have to do any parsing of lines or data because the Serializer does it all for you! RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

              C Offline
              C Offline
              campbells
              wrote on last edited by
              #10

              does it need any loop?? cause wat in my mind is the game maybe have 1-4 player, so let said in this round have 3 players player:1 NAme:A Position:10 Movestep:2 player:2 NAme:B Position:8 Movestep:2 player:3 NAme:C Position:7 Movestep:2 -- so i was thinking to use the x value to loop 3 times since got 3 player and read 4 line accordinly.... am not sure can it work but i only know a bit on using the readline, i never write it in xml b4..not really understand how it work ..

              D 1 Reply Last reply
              0
              • C campbells

                does it need any loop?? cause wat in my mind is the game maybe have 1-4 player, so let said in this round have 3 players player:1 NAme:A Position:10 Movestep:2 player:2 NAme:B Position:8 Movestep:2 player:3 NAme:C Position:7 Movestep:2 -- so i was thinking to use the x value to loop 3 times since got 3 player and read 4 line accordinly.... am not sure can it work but i only know a bit on using the readline, i never write it in xml b4..not really understand how it work ..

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #11

                campbells wrote:

                does it need any loop??

                Nope. You don't need to worry about how many players are in the file or parsing each and every line. The Serializer takes care of all of this for you. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                C 2 Replies Last reply
                0
                • D Dave Kreskowiak

                  campbells wrote:

                  does it need any loop??

                  Nope. You don't need to worry about how many players are in the file or parsing each and every line. The Serializer takes care of all of this for you. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                  C Offline
                  C Offline
                  campbells
                  wrote on last edited by
                  #12

                  do u have more example or article about this xml thing?

                  1 Reply Last reply
                  0
                  • D Dave Kreskowiak

                    campbells wrote:

                    does it need any loop??

                    Nope. You don't need to worry about how many players are in the file or parsing each and every line. The Serializer takes care of all of this for you. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                    C Offline
                    C Offline
                    campbells
                    wrote on last edited by
                    #13

                    i wonder is i have 3 player i do i read the first line to get how many player at that time??? cause i jsut understand is will recall the player status part but how it know how many times to loop it?

                    D 1 Reply Last reply
                    0
                    • C campbells

                      i wonder is i have 3 player i do i read the first line to get how many player at that time??? cause i jsut understand is will recall the player status part but how it know how many times to loop it?

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #14

                      Your not listening... Doing it using a Serialization method frees you having to loop anything while reading the file. The Serializer takes care of that for you!! All you need to do is check the Bounds of the array to get the number of Players in it after the Players array is recreated. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups