Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a specific line from txt file 1

Status
Not open for further replies.

leprogrammer

Programmer
Mar 21, 2008
25
DK
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)
 
Read the entire contents of your file into either a datatable or a dictionary of classes.

A file should only be used to save data and retrieve it when absolutely necessary. If you are expecting to use it like a database then you will run into a nightmare. Load all your content into your app and use it that way. If it's a huge file then I would recommend creating a database with the data. MySQL and SQL Server Express are easy to use and free.

 
Yes I am aware of txt files not should be used as a database

But this program is for a game that uses .txt files, so I cant rellay use SQL or xml.
 
So in that case, just load the lines into classes and create a dictionary with the ID as the key and the Class as the value.

public class Character
{
public int ID;
public string Name;
public int HitPoints;
public int AttackStrength;
}


Read each line and parse the information you need.

public class CharacterLibrary
{
public Dictionary<int, Character> Characters = new Dictionary<int, Character>();

public void ParseFile(string filename)
{
//Start reading each line using your while loop
//After reading a line do the following...

string[] input = line.Split(" ");

Character c = new Character();
c.ID = input[0];
c.Name = input[1];
...

Characters.Add(c.ID, c);
}


Now from your program you can get an item by ID quite easily.

CharacterLibrary library = new CharacterLibrary();

library.ParseFile(@"C:\gamefile.txt");

Character c = library.Characters[3];



If you need to write back out to the file - I suggest looping through each Character and have them return a string to write. You could override the ToString() method and have it spit out the format that you are expecting

pubic override string ToString()
{
return this.ID + " " + this.Name + " " ....;
}



 
I tryed as you said

But I cant get my class working

Here is what it looks like right now:
using System.Collections.Generic;
using System.IO;

namespace MonstersetBaseEditor
{
public class Character
{
public int ID;
public string Name;
public int HitPoints;
public int AttackStrength;
}

public class CharacterLibrary
{
public Dictionary<int, Character> Characters = new Dictionary<int, Character>();

public void ParseFile(string filename)
{
string monsterLocation = "D:/msbEditor/monster.txt";

using (StreamReader sr = new StreamReader(@monsterLocation))
{
string line;

while ((line = sr.ReadLine()) != null)
{
string[] input = line.Split(" ");
}

sr.Close();
}

Character c = new Character();
c.ID = input[0];
c.Name = input[1];

Characters.Add(c.ID, c);
}
}
}

But cant seem to follow what you mean

Can you explain a bit and correct my code?

Thanks
 
Code:
public void ParseFile(string filename)
        {
            string monsterLocation = @"D:\msbEditor\monster.txt"; //Note the direction of the slashes and the @ symbol in front.

            using (StreamReader sr = new StreamReader(monsterLocation))
            {
                string line;

                while ((line = sr.ReadLine()) != null)
                {
                    string[] input = line.Split(" ");

//Each character is on a line - so once you finish reading the line, create your character. This will populate your dictionary.

                    Character c = new Character();
                    c.ID = input[0];
                    c.Name = input[1];

                    Characters.Add(c.ID, c);
                }

                sr.Close();
            }
        }

 
Thanks but 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..
 
Yes, but right now I cant rellay cope with classes.

For my next program I use classes ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top