johnnie
johnnie

Reputation: 2057

Seeking for a line i a text file

I need some assistance, I am writing a method to read a text file, and if any exception occurs I append a line to the text file. e.g "**"

So what I need to know is how can I check for that specific line of text in the text file without reading every line of the text file, like a peek method or something.

Any help would be appreciated.

Thanks in advance.

Upvotes: 1

Views: 5932

Answers (4)

johnnie
johnnie

Reputation: 2057

I have found a solution, the line I have appended to the file will always be the last line in the file, so I created a method to read the last line. See below:

public string ReadLastLine(string path)
        {
            string returnValue = "";
            FileStream fs = new FileStream(path, FileMode.Open);
            for (long pos = fs.Length - 2; pos > 0; --pos)
            {
                fs.Seek(pos, SeekOrigin.Begin);               
                StreamReader ts = new StreamReader(fs);
                returnValue = ts.ReadToEnd();
                int eol = returnValue .IndexOf("\n");
                if (eol >= 0)
                {                    
                    fs.Close();
                    return returnValue .Substring(eol + 1);
                }                
            }
            fs.Close();
            return returnValue ;
        }

Upvotes: 1

Daniel van Os
Daniel van Os

Reputation: 41

From what I understand you want to process some files and after the processing find out which files contain the "**" symbol, without reading every line of the file.

If you append the "**" to the end of the file you could do something like:

using (StreamReader sr = new StreamReader(File.OpenText(fileName)))
{
    sr.BaseStream.Seek(-3, SeekOrigin.End);
    string endToken = sr.ReadToEnd();
    if (endToken == "**\n")
    {
        // if needed, go back to start of file:
        sr.BaseStream.Seek(0, SeekOrigin.Begin);
            // do something with the file
    }
}

Upvotes: 0

Roland Mai
Roland Mai

Reputation: 31077

You will need to maintain a separate file with indexes (such as comma delimited) of where your special markers are. Then you can only read those indexes and use the Seek method to jump to that point in the filestream.

If your file is relatively small, let's say <50MB this is an overkill. More than that you can consider maintaining the index file. You basically have to weigh the performance of an extra IO call (that is reading the index file) with that of simply reading from the filestream each line.

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460138

You can use File.ReadLines in combination with Any:

bool isExcFile = System.IO.File.ReadLines(path).Any(l => l == "**");

The ReadLines and ReadAllLines methods differ as follows: When you use ReadLines, you can start enumerating the collection of strings before the whole collection is returned; when you use ReadAllLines, you must wait for the whole array of strings be returned before you can access the array. Therefore, when you are working with very large files, ReadLines can be more efficient.

Upvotes: 2

Related Questions