Click here to Skip to main content
15,886,780 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am parsing a file for various fields using the following code

C#
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
    string line = sr.ReadLine();
    int startIndex, endIndex = -1;

    while (line != null)
    {
        if (line.IndexOf("value") > -1)
        {
            startIndex = line.IndexOf("value") + 5;

............
............

Instead of parsing for all the fields I need I would like to parse for one specific value, if that value exists and its normally somewhere at the end of the file I would then like to return to the top of the file and parse for the rest of the values. Otherwise I don't want to waste my time parsing for all the values if this one value does not exist.

My problem is how do I return to the top of the file and start parsing all over again if my found value is there?


Thanks
Posted

Use a BufferedStream[^], which allows you to seek to different positions within the file, including the beginning.
 
Share this answer
 
I ended up using this once I found it.

C#
stream.DiscardBufferedData();
stream.BaseStream.Seek(0, SeekOrigin.Begin);
 
Share this answer
 
Comments
BillWoodruff 3-May-15 2:10am    
I wonder if this is a very "expensive" (time, memory overhead) method compared to using a BufferedStream, as Richard M. suggests.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900