4 Replies - 20242 Views - Last Post: 25 January 2009 - 01:34 PM Rate Topic: -----

#1 Dumpen  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 21-March 08

Reading a specific line from txt file

Posted 23 March 2008 - 08:57 AM

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)

Is This A Good Question/Topic? 0
  • +

Replies To: Reading a specific line from txt file

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Reading a specific line from txt file

Posted 23 March 2008 - 10:37 AM

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
Was This Post Helpful? 0
  • +
  • -

#3 Dumpen  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 21-March 08

Re: Reading a specific line from txt file

Posted 23 March 2008 - 11:00 AM

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..
Was This Post Helpful? 1

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Reading a specific line from txt file

Posted 23 March 2008 - 02:29 PM

That will most certainly work just fine. Good job.
Was This Post Helpful? 0
  • +
  • -

#5 Angel Essence  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-January 09

Re: Reading a specific line from txt file

Posted 25 January 2009 - 01:34 PM

ummm I have the project and I'm a beginner in the programming

I've added the code with few changes which:

private void findMonster(int StudentId)
		{
			StreamReader sr = new StreamReader(@"---------\new2.txt");
			int searchId = StudentId;
			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();
			txtnew2.Text = name;

my only problem is in the last line
" txtnew2.Text = name;" it keeps giving me a blue line for txtnew2 Y?
plz help as fast as possible
thanks a lot ^^
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1