Hi all,

My question pertains to the reading of a text document. Currently I am reading a text file searching for certain syntax and once it finds it then it uses that line. The thing is, I am currently doing this:

while (reader.readLine() != null) { 
    DOSTUFF();
}

This is nice and it works well, but it's somewhat sluggish because on every text file it needs to go through 871 lines first before it will find the correct syntax line.

I was wondering if there is a way to SKIP reading through each line and go directly to line 872? IE instead of reading 1 line at a time and searching for the syntax, it will just reader.goToAndRead(872);.... this would save me an enormous amount of time in my application.

Thank you for any help you offer!

You should be able to iterate through 872 lines in no time. You still have to read the line in to memory and search for a carriage return to count the line numbers. Without knowing the byte position of where you want the streem to seek to then you really should continue with your current approach. I guess you could shave a few CPU cycles off if you ignored the line contents and searched for the carriage return but that would only have an impact if you were dealing with files that have very long lines I would think.

Here is code to test that:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace daniweb
{
  public partial class frmSeek : Form
  {
    const string fname = @"C:\fs.txt";
    public frmSeek()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      using (FileStream fs = new FileStream(fname, FileMode.CreateNew))
      {
        for (int i1 = 0; i1 < 1000; i1++)
        {
          byte[] buffer = System.Text.ASCIIEncoding.ASCII.GetBytes(new string('x', 500) + Environment.NewLine);
          fs.Write(buffer, 0, buffer.Length);
        }
        fs.Flush();
        fs.Close();
      }
      MessageBox.Show("done");
    }

    private void button2_Click(object sender, EventArgs e)
    {
      using (StreamReader sr = new StreamReader(fname))
      {
        string line;
        int lineNbr = 0;
        DateTime dtStart = DateTime.Now;
        while ((line = sr.ReadLine()) != null)
        {
          lineNbr++;
          if (lineNbr >= 872)
          {
            DateTime dtStop = DateTime.Now;
            TimeSpan ts = dtStop.Subtract(dtStart);
            Console.WriteLine("Found line #872 in: {0}", ts.TotalMilliseconds);
            break;
          }
        }
        sr.Close();
      }
    }
  }
}

The streamreader was able to navigate to line 872 in less than a second.

Comments
Detailed response with test code. Thanks

As sknake pointed out, you would probably need to count the 872 lines by reading them in, but if you have lines that are fixed length, you could calculate the position within the stream ( lineLength * 872 ) and then use the Stream.Seek method to position the file pointer to the location you want to begin searching.

In addition, if you know each of these files will contain a certain block size (number of bytes) that you always want to skip, you could use the above to seek past that position. Furthermore, if you know that your search criteria will never be past a particular block size (number of bytes), you can evaluate the Stream.Position property to determine you have read past this point.

Edited 6 Years Ago by DdoubleD: n/a

hi jeeter..would be good if you could share your current codes with me..as im also doing a project similar to this..thank you so much!

This question has already been answered. Start a new discussion instead.