Taurent

hello to all

well i need to read a file txt but i need to read some selected lines of the file, for example:

hello

my

friend

the time is

9:59am

and i need only read the 3rd line. im using this to read all the text:

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

StreamReader readr = File.OpenText("c:/leeme.txt");

while (readr.EndOfStream == false)

{

string line = "";

char[] arr;

line = readr.ReadLine();

while ((line = readr.ReadLine()) != null)

{

arr = line.ToCharArray(0,line.Length);

Console.WriteLine(arr);

}

}

Console.ReadLine();

}

}

}

with this i can read all the file, the problem is that i only want that specific line, how i can make that

tnks for the time.

farewell...




Re: .NET Base Class Library how can i read a specific line of a txt file?

Brendan Grant

Unless the preceding lines are of a definite and unchanging size... there really is no way for you read the third line without first reading lines one and two.

Worst case you just add a couple of calls to readr.ReadLine() to read them and ignore the results and only care about the return from call #3.






Re: .NET Base Class Library how can i read a specific line of a txt file?

Taurent

yeah i was thinking the same Tongue Tied, well ... i will wait a while to see if someone know how to do it.

well i also using a static format for the file, so the texts are allways goin to be in the place i want, so i think the old read 3 times way is maybe the best Tongue Tied

tnks any way

farewell






Re: .NET Base Class Library how can i read a specific line of a txt file?

Brendan Grant

Think of it this way... Lines donĄ¯t matter in a text file... in fact they really donĄ¯t exist. When you view a text file the only reason you see lines is because of special new line characters in the file... the raw file looks more like this:

hello\r\nmy\r\nfriend\r\nthe time is\r\n9:59am

where each one of those \r\n combinations (escape sequences) denotes a new line and a return of the cursor to the start of the line... when you are reading that file programmatically... there are no lines... itĄ¯s just a long stream of characters and ReadLine() simply works by reading data, character by character until it encounters a new line character and then notes that location (for use with the next read) and spits back to you all of the text since the last read up until this point.

If you are using a defined format for your file... not so much in terms of line positions but character counts... you could manually set the Position property of your reader, you could then do a Read() or ReadLine() from that point and bypass the first two ReadLines()Ą­ but again, that only works if you know what character line #3 begins on and that that value never changes.






Re: .NET Base Class Library how can i read a specific line of a txt file?

OmegaMan

Taurent wrote:
with this i can read all the file, the problem is that i only want that specific line, how i can make that


Admittedly this has overhead involved, but if the files are small enough here is the ability to read a specific line.

Console.WriteLine((File.ReadAllLines(@"C:\debug.log"))[2]);






Re: .NET Base Class Library how can i read a specific line of a txt file?

grog

You should be able to do a seek on the file stream. Something like the following:

readr.BaseStream.Seek(20, 0);

I just put 20 in as an offset for demonstration, but if you are trying to read the third record you would put an offset of two times the recordlength to get the third record then read the for the length of a record.