Reading a specific line from txt file
-
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)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 -
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%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i?TV.C\y -------------------------------------------------------- 128 bit encrypted signature, crack if you can
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.
-
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.
-
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%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i?TV.C\y -------------------------------------------------------- 128 bit encrypted signature, crack if you can
That's why Split should have the ability to honor quote characters... as mine does.
-
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.
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.
-
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)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| -
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|XML is text. CSV is text.
-
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.
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%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i?TV.C\y -------------------------------------------------------- 128 bit encrypted signature, crack if you can
-
XML is text. CSV is text.
I said txt not text :)
-
I said txt not text :)
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.