Question

So I'm doing a project where I am reading in a config file. The config file is just a list of string like "D 1 1", "C 2 2", etc. Now I haven't ever done a read/write in C# so I looked it up online expecting to find some sort of rendition of C/C++ .eof(). I couldn't find one.

So what I have is...

TextReader tr = new StreamReader("/mypath");

Of all the examples online of how I found to read to the end of a file the two examples that kept occurring were

while ((line = tr.ReadLine() != null)

or

while (tr.Peek() >= 0)

I noticed that StreamReader has a bool EndOfStream but no one was suggesting it which led me to believe something was wrong with that solution. I ended up trying it like this...

while (!(tr as StreamReader).EndOfStream)

and it seems to work just fine.

So I guess my question is would I experience issues with casting a TextReader as a StreamReader and checking EndOfStream?

Was it helpful?

Solution

One obvious downside is that it makes your code StreamReader specific. Given that you can easily write the code using just TextReader, why not do so? That way if you need to use a StringReader (or something similar) for unit tests etc, there won't be any difficulties.

Personally I always use the "read a line until it's null" approach - sometimes via an extension method so that I can use

foreach (string line in reader.EnumerateLines())
{
}

EnumerateLines would then be an extension method on TextReader using an iterator block. (This means you can also use it for LINQ etc easily.)

OTHER TIPS

Or you could use ReadAllLines, to simplify your code:

http://msdn.microsoft.com/en-us/library/s2tte0y1.aspx

This way, you let .NET take care of all the EOF/EOL management, and you focus on your content.

No you wont experience any issue's. If you look at the implementation if EndToStream, you'll find that it just checks if there is still data in the buffer and if not, if it can read more data from the underlying stream:

public bool EndOfStream
{
    get
    {
        if (this.stream == null)
        {
            __Error.ReaderClosed();
        }
        if (this.charPos < this.charLen)
        {
            return false;
        }
        int num = this.ReadBuffer();
        return num == 0;
    }
}

Ofcourse casting in your code like that makes it dependend on StreamReader being the actual type of your reader which isn't pretty to begin with.

Maybe read it all into a string and then parse it: StreamReader.ReadToEnd()

using (StreamReader sr = new StreamReader(path)) 
{
  //This allows you to do one Read operation.
  string contents = sr.ReadToEnd());
}

Well, StreamReader is a specialisation of TextReader, in the sense that StreamReader inherits from TextReader. So there shouldn't be a problem. :)

            var arpStream = ExecuteCommandLine(cmd, arg);
            arpStream.ReadLine(); // Read entries
            while (!arpStream.EndOfStream)
            {
                var line1 = arpStream.ReadLine().Trim();
                //   TeststandInt.SendLogPrint(line, true);
            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top