Hi all,

i'm very new in this programming world..so i hope i can get some help here to understand more about C#.
my task is to extract out the data from specific column and row in text file. i do found 1 post from this from regarding this matter..but there is one part that i didn't understand so i hope u guyz can help me out. Capture3

hope this is the right question to asked. i really2 new in this..

thx in advance.

-nik-

First off you should use copy and paste to insert your code, instead of a picture. That way someone can copy your code and make adjustments, without having to type the whole thing.

The line 2 should probably use the EOF constant, instead of null

Your string variables should be after line 17 and url is already a string, so it's redundant to use the ToString() method. All of that, lines 13-18, should go in the brackets at line 8

Do the lines in your text file, all contain the same number of words?

hi all..

it has been awhile..sorry for the inconvenience..

thx for the reply..i try to use the solution provided, but it doesn't seem to work..maybe because i made a mistake..but dont know where..

here are the code:

StreamReader tr = new StreamReader("C:\\Floor Schedule3.txt");

string line = tr.ReadLine();
int curLine = 0;

while (line != null)
{
    curLine++
    if (curLine == 0)
      {                         
        string name = line.Split('\t')[0];
        string url = line.Split('\t')[1];
        string desc = line.Split('\t')[2];

        line = reader.ReadLine();
        TextBox1.Text = url.ToString();
      }
}

the attachment is the text file sample that i use.

Thx in advance.

-Nik-

Of course your code doesn't work, you've told it not to do so. Think about what happens on each line. For example, you set curLine to 0 on line 4. Then on line 8 you increment curLine. Line 9 you ask "is curLine equal to zero?". It never will be (well, it could be but your text file would have to contain over 4 billion lines).

What line do you really want to get data from?

You've also put the "read the next line" code inside the if statement, so the next line will never be read. You'll just loop looking at the first line until you overflow curLine, then count from MinInt to 0. Then it will pull data from the first line.

Taking a better look at it, it won't even compile as you've never defined 'reader'.

i see..i'm very new in this..
i would like to call out the data in line 3 where the area value stated. (113 m)

opss..my mistake..i copy all the code even i already put // on it..the 'reader' is really a mistake..it's actually tr.readline..

StreamReader tr = new StreamReader("C:\\Floor Schedule2.txt");

                     string line = tr.ReadLine();
                     int curLine = 0;

                     while (line != null)
                     {
                         curLine++;

                         if (curLine == 3)
                         {                            

                             string name = line.Split('\t')[0];
                             string test = line.Split('\t')[1];
                             string desc = line.Split('\t')[2];

                             TextBox1.Text = test.ToString();
                          }
                      }

One thing I noticed, the file you uploaded has a different name than the one in your code.

I pared things down for you got rid of alot of the extra code you weren't using.

using System.IO;

//Load the whole file into a list.  As long as you don't plan on using really long files this is much  simpler
List<string> line = File.ReadAllLines(@"C:\Floor_Schedule2.txt").ToList<string>();

//extract the info from the line you want
string test = line[3].Split('\t')[1];

//put the info into the text box
TextBox1.Text = test;

The output is "Level 1"
If you don't want the quotes use TextBox1.Text = test.Trim('"');

Your code will read the first line of your text file.
If the line is read you will enter the while loop(I would use while (tr.Peek() >= 0) here)
In the loop you never call ReadLine again, so this will loop forever. Insert line = tr.ReadLine(); between lines 18 and 19. This code still needs improvement, but it is a start.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.