954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

filestream in c#

i have to read 10 lines from the middle of a file, i tried to use seek method but it requires number of bytes. i dont know how many bytes will be contained in 10lines. how can i tell seek to read 10 lines from a specified location.

ayesha25
Newbie Poster
7 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

If the file you're trying to read is a textfile, then you can use the TextReader's ReadLine method.

Try this and see if it helps.

TextReader reader = New StreamReader("<file to read>");
string line = "";

int startReadingHere = <enter line number here>;
int lineIndex = 0;
int readLines = 0;

while (reader.Peek() != -1)
{
   if (lineIndex >= startReadingHere && readLines < 10)
   {
      line = reader.ReadLine();
      //Do something with the string
      readLines += 1;
   }
   lineIndex += 1;
}
Oxiegen
Master Poster
715 posts since Jun 2006
Reputation Points: 87
Solved Threads: 141
 

If this has been helpful, please mark this thread as solved.

Oxiegen
Master Poster
715 posts since Jun 2006
Reputation Points: 87
Solved Threads: 141
 

At 20 days old, i'm not sure it was worth resurrecting this thread to have it marked solved

Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You