Welcome to Dream.In.Code
Become a C# Expert!

Join 150,422 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,099 people online right now. Registration is fast and FREE... Join Now!




Reading a specific line from txt file

 
Reply to this topicStart new topic

Reading a specific line from txt file

Dumpen
23 Mar, 2008 - 07:57 AM
Post #1

New D.I.C Head
*

Joined: 21 Mar, 2008
Posts: 40

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


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)
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Reading A Specific Line From Txt File
23 Mar, 2008 - 09:37 AM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,327



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
You could read the file (using a StreamReader object) line by line in a loop and put the result into a String. Then you could use the SubString or CharAt method to extract the id on each line comparing it to the value that you are searching for.

StreamReader.ReadLine
User is offlineProfile CardPM
+Quote Post

Dumpen
RE: Reading A Specific Line From Txt File
23 Mar, 2008 - 10:00 AM
Post #3

New D.I.C Head
*

Joined: 21 Mar, 2008
Posts: 40

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..
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Reading A Specific Line From Txt File
23 Mar, 2008 - 01:29 PM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,327



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
That will most certainly work just fine. Good job.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 08:24PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month