Solved

re reading the last line again with StreamReader.ReadLine()

Posted on 2008-06-13
4
3,582 Views
Last Modified: 2012-05-05
In the example below I need to use sr.ReadLine() to read the lines out of a text file. Sometimes I may want to back the position of the streamreader up to the beginning of the previous line, so that the next call to sr.ReadLine() reads the previous line again. sr.BaseStream.Seek doesn't seem to be working for me and the sr.BaseStream.Position doesn't seem to reflect what I expect.
string filePath = @"c:\temp\debug.txt";
 

using (StreamReader sr = new StreamReader(filePath))

{

    string line1 = sr.ReadLine();

    string line2 = sr.ReadLine();
 

    // Back up so the next ReadLine reads line 2 again

    sr.BaseStream.Seek(-line2.Length, SeekOrigin.Current);

    sr.DiscardBufferedData();
 

    string line2Again = sr.ReadLine();
 

    if (line2 != line2Again)

    {

        Console.Write("This is not what I expect");

    }

}

Open in new window

0
Comment
Question by:southparksystems
4 Comments
 
LVL 11

Accepted Solution

by:
dbrckovi earned 250 total points
ID: 21780458
Hi!

You could load the complete file in some kind of collection, and work with it.
Maybe something like this:


    public partial class Form1 : Form

    {

        StreamReader SR;

        List<string> FileLines = new List<string>();
 

        public Form1()

        {

            InitializeComponent();

        }
 

        private void button1_Click(object sender, EventArgs e)

        {

            MessageBox.Show("First line: " + FileLines[0]);

            MessageBox.Show("Last line: " + FileLines[FileLines.Count - 1]);

            MessageBox.Show("Line before last: " + FileLines[FileLines.Count - 2]);

        }
 

        private void Form1_Load(object sender, EventArgs e)

        {

            //load all lines

            SR = File.OpenText("c:\\aaa\\aaa.txt");

            string CurrentLine = "";

            do

            {

                CurrentLine = SR.ReadLine();

                if (CurrentLine != null && CurrentLine.Length != 0)

                {

                    FileLines.Add(CurrentLine);

                }

            } while (CurrentLine != null && CurrentLine.Length != 0);
 

            SR.Close();

            MessageBox.Show("Lines in file: " + FileLines.Count);

        }

    }

Open in new window

0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 250 total points
ID: 21780482
ReadLine() strips off the carriage return/line feed at the end of the line...so you need to compensate for that.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ConsoleSql 1 34
Achieve json result 2 65
How to LOAD up a .NET Framework website with data? 2 40
How to get the web content with different encodings 2 27
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now