CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Threaded View

  1. #1
    Join Date
    Mar 2008
    Posts
    24

    Reading a specific line from txt file

    Hello.

    I have a text file that looks like this:
    Code:
    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
    3	1	"Spider"		2	30	0	4	7	1	0	8	1	2	0	1	5	400	1800	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)

    EDIT:
    I just used
    Code:
            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..
    Last edited by Dumpen; March 23rd, 2008 at 12:58 PM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured