Read line in textfile

  • Thread starter Thread starter Tim Bücker
  • Start date Start date
T

Tim Bücker

Hello.

Is there a way to read a specified line in a textfile?
Something like TextReader.ReadTextLine(4);

It seems very odd to use
reader.ReadLine();
reader.ReadLine();
reader.ReadLine();
string wishedLine = reader.ReadLine();

Is there a better way?
Greetings and thanks in advance,
Tim.
 
Tim Bücker said:
Is there a way to read a specified line in a textfile?
Something like TextReader.ReadTextLine(4);

It seems very odd to use
reader.ReadLine();
reader.ReadLine();
reader.ReadLine();
string wishedLine = reader.ReadLine();

Is there a better way?

Well, you could write:

void SkipLines (TextReader reader, int lines)
{
for (int i=0; i < lines; i++)
{
reader.ReadLine();
}
}

and then do:

SkipLines (reader, 3);
string line = reader.ReadLine();

There's no other way to do it.
 
Or if you require random access to the lines, you should read the lines into
an ArrayList and then access them using an index.

ArrayList lines = new ArrayList();
string line;

while ((line = reader.ReadLine()) != null)
lines.Add(line);

string wishedLine = (string) lines[3];

HTH,
Stefan

Tim Bücker said:
Is there a way to read a specified line in a textfile?
Something like TextReader.ReadTextLine(4);

It seems very odd to use
reader.ReadLine();
reader.ReadLine();
reader.ReadLine();
string wishedLine = reader.ReadLine();

Is there a better way?

Well, you could write:

void SkipLines (TextReader reader, int lines)
{
for (int i=0; i < lines; i++)
{
reader.ReadLine();
}
}

and then do:

SkipLines (reader, 3);
string line = reader.ReadLine();

There's no other way to do it.
 
Back
Top