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. C#
  4. Reading a specific line from txt file

Reading a specific line from txt file

Scheduled Pinned Locked Moved C#
questiontoolstutorial
12 Posts 5 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 Offline
    C Offline
    Casper Hansen
    wrote on last edited by
    #1

    Hello. I have a text file that looks like this: 0 1 "Bull Fighter" 6 100 0 16 20 6 0 28 6 3 0 1 5 400 1600 10 2 130 10 6 0 0 0 0 0 1 1 "Hound" 9 140 0 22 27 9 0 39 9 3 0 1 5 400 1600 10 2 130 10 6 0 0 0 0 0 2 1 "Budge Dragon" 4 60 0 10 13 3 0 18 3 3 0 1 4 400 2000 10 2 120 10 6 0 0 0 0 0 It is called monster.txt The first value is a id which I also use in my script. Lets say I have the id 3, this would be "Spider" since the first number of "Spider" is 3 What is the best way to read the monster.txt file for the id and retrive the name? (Name = "Spider", id = 3) (As my example above)

    X D C 3 Replies Last reply
    0
    • C Casper Hansen

      Hello. I have a text file that looks like this: 0 1 "Bull Fighter" 6 100 0 16 20 6 0 28 6 3 0 1 5 400 1600 10 2 130 10 6 0 0 0 0 0 1 1 "Hound" 9 140 0 22 27 9 0 39 9 3 0 1 5 400 1600 10 2 130 10 6 0 0 0 0 0 2 1 "Budge Dragon" 4 60 0 10 13 3 0 18 3 3 0 1 4 400 2000 10 2 120 10 6 0 0 0 0 0 It is called monster.txt The first value is a id which I also use in my script. Lets say I have the id 3, this would be "Spider" since the first number of "Spider" is 3 What is the best way to read the monster.txt file for the id and retrive the name? (Name = "Spider", id = 3) (As my example above)

      X Offline
      X Offline
      Xmen Real
      wrote on last edited by
      #2

      Split() will work but you have space in name too, i will suggest you to convert your line like this, if possible : 0~1~"Bull Fighter"~6~100~0~16~20~6~0~28~6~3~0~1~5~400~1600~10~2~130~10~6~0~0~0~0~0 then you can use Split('~') hope this will help

      TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87??6?N8?BcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN% Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i?TV.C\y -------------------------------------------------------- 128 bit encrypted signature, crack if you can

      C P 2 Replies Last reply
      0
      • C Casper Hansen

        Hello. I have a text file that looks like this: 0 1 "Bull Fighter" 6 100 0 16 20 6 0 28 6 3 0 1 5 400 1600 10 2 130 10 6 0 0 0 0 0 1 1 "Hound" 9 140 0 22 27 9 0 39 9 3 0 1 5 400 1600 10 2 130 10 6 0 0 0 0 0 2 1 "Budge Dragon" 4 60 0 10 13 3 0 18 3 3 0 1 4 400 2000 10 2 120 10 6 0 0 0 0 0 It is called monster.txt The first value is a id which I also use in my script. Lets say I have the id 3, this would be "Spider" since the first number of "Spider" is 3 What is the best way to read the monster.txt file for the id and retrive the name? (Name = "Spider", id = 3) (As my example above)

        D Offline
        D Offline
        Dave Sexton
        wrote on last edited by
        #3

        Depending on how often you need to access this information you'd probably be better off storing this information in a DataTable (which you could create at runtime). You can then use the Rows property of the DataTable object to retrieve your row at the given index. I also agree with the previous poster that you should use a different char for separating values.

        But fortunately we have the nanny-state politicians who can step in to protect us poor stupid consumers, most of whom would not know a JVM from a frozen chicken. Bruce Pierson
        Because programming is an art, not a science. Marc Clifton

        1 Reply Last reply
        0
        • X Xmen Real

          Split() will work but you have space in name too, i will suggest you to convert your line like this, if possible : 0~1~"Bull Fighter"~6~100~0~16~20~6~0~28~6~3~0~1~5~400~1600~10~2~130~10~6~0~0~0~0~0 then you can use Split('~') hope this will help

          TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87??6?N8?BcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN% Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i?TV.C\y -------------------------------------------------------- 128 bit encrypted signature, crack if you can

          C Offline
          C Offline
          Casper Hansen
          wrote on last edited by
          #4

          Its not a problem getting the monster name. When I just have the line, it is no problem. The problem is reading the file and accessing the line through a id. Because it would be pretty silly to read the whole file just for 1 line.

          G P X 3 Replies Last reply
          0
          • C Casper Hansen

            Its not a problem getting the monster name. When I just have the line, it is no problem. The problem is reading the file and accessing the line through a id. Because it would be pretty silly to read the whole file just for 1 line.

            G Offline
            G Offline
            Gareth H
            wrote on last edited by
            #5

            Casper Hansen, You cannot read only 1 line of a text file. You need to get the whole file, and then search for your ID. Regards, Gareth.

            1 Reply Last reply
            0
            • X Xmen Real

              Split() will work but you have space in name too, i will suggest you to convert your line like this, if possible : 0~1~"Bull Fighter"~6~100~0~16~20~6~0~28~6~3~0~1~5~400~1600~10~2~130~10~6~0~0~0~0~0 then you can use Split('~') hope this will help

              TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87??6?N8?BcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN% Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i?TV.C\y -------------------------------------------------------- 128 bit encrypted signature, crack if you can

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              That's why Split should have the ability to honor quote characters... as mine does.

              1 Reply Last reply
              0
              • C Casper Hansen

                Its not a problem getting the monster name. When I just have the line, it is no problem. The problem is reading the file and accessing the line through a id. Because it would be pretty silly to read the whole file just for 1 line.

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                As presented you have to read the entire file. Or use an index in a separate file. Or alter the format somehow. For instance each line could begin with the length of the line. Personally, I suggest using XML, but that's just me.

                1 Reply Last reply
                0
                • C Casper Hansen

                  Hello. I have a text file that looks like this: 0 1 "Bull Fighter" 6 100 0 16 20 6 0 28 6 3 0 1 5 400 1600 10 2 130 10 6 0 0 0 0 0 1 1 "Hound" 9 140 0 22 27 9 0 39 9 3 0 1 5 400 1600 10 2 130 10 6 0 0 0 0 0 2 1 "Budge Dragon" 4 60 0 10 13 3 0 18 3 3 0 1 4 400 2000 10 2 120 10 6 0 0 0 0 0 It is called monster.txt The first value is a id which I also use in my script. Lets say I have the id 3, this would be "Spider" since the first number of "Spider" is 3 What is the best way to read the monster.txt file for the id and retrive the name? (Name = "Spider", id = 3) (As my example above)

                  C Offline
                  C Offline
                  Casper Hansen
                  wrote on last edited by
                  #8

                  I just used private void findMonster(int monsterId) { StreamReader sr = new StreamReader(@monsterLocation); int searchId = monsterId; int actualId = 0; string name = "(Not found)"; string[] details = null; string line = null; while ((line = sr.ReadLine()) != null) { line = line.Trim(); if (line == "") continue; details = line.Split('\t'); actualId = int.Parse(details[0]); if (actualId == searchId) { name = details[2].Replace("\"", ""); break; } } sr.Close(); txtMonster.Text = name; } Dont know if its the best way, but it works.. As you guys said I should use some other database form then txt, but the game im devolping this software to is using txt files so cant rellay change it X|

                  P 1 Reply Last reply
                  0
                  • C Casper Hansen

                    I just used private void findMonster(int monsterId) { StreamReader sr = new StreamReader(@monsterLocation); int searchId = monsterId; int actualId = 0; string name = "(Not found)"; string[] details = null; string line = null; while ((line = sr.ReadLine()) != null) { line = line.Trim(); if (line == "") continue; details = line.Split('\t'); actualId = int.Parse(details[0]); if (actualId == searchId) { name = details[2].Replace("\"", ""); break; } } sr.Close(); txtMonster.Text = name; } Dont know if its the best way, but it works.. As you guys said I should use some other database form then txt, but the game im devolping this software to is using txt files so cant rellay change it X|

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    XML is text. CSV is text.

                    C 1 Reply Last reply
                    0
                    • C Casper Hansen

                      Its not a problem getting the monster name. When I just have the line, it is no problem. The problem is reading the file and accessing the line through a id. Because it would be pretty silly to read the whole file just for 1 line.

                      X Offline
                      X Offline
                      Xmen Real
                      wrote on last edited by
                      #10

                      read all lines from text then do your operation, to read all line here is the shortest ways :

                      string[] lines = File.ReadAllLines( <FILEPATH> );

                      hope this will help

                      TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87??6?N8?BcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN% Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i?TV.C\y -------------------------------------------------------- 128 bit encrypted signature, crack if you can

                      1 Reply Last reply
                      0
                      • P PIEBALDconsult

                        XML is text. CSV is text.

                        C Offline
                        C Offline
                        Casper Hansen
                        wrote on last edited by
                        #11

                        I said txt not text :)

                        P 1 Reply Last reply
                        0
                        • C Casper Hansen

                          I said txt not text :)

                          P Offline
                          P Offline
                          PIEBALDconsult
                          wrote on last edited by
                          #12

                          Yeah, so? "txt" is not a reserved extension for some special format. Likewise the extension of your file could just as easily be "dat" or "casper". I merely meant to suggest that you not use the format shown if at all possible.

                          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