简体   繁体   English

使用 StreamReader.EndOfStream 的问题?

[英]Issues using StreamReader.EndOfStream?

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().配置文件只是一个字符串列表,如“D 1 1”、“C 2 2”等。现在我从未在 C# 中进行过读/写,所以我在网上查了一下,希望能找到某种形式的演绎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.我注意到 StreamReader 有一个 bool EndOfStream 但没有人建议它,这让我相信该解决方案有问题。 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?所以我想我的问题是我是否会遇到将 TextReader 转换为 StreamReader 并检查 EndOfStream 的问题?

One obvious downside is that it makes your code StreamReader specific.一个明显的缺点是它使您的代码StreamReader特定。 Given that you can easily write the code using just TextReader , why not do so?鉴于您可以仅使用TextReader轻松编写代码,为什么不这样做呢? That way if you need to use a StringReader (or something similar) for unit tests etc, there won't be any difficulties.这样,如果您需要使用StringReader (或类似的东西)进行单元测试等,就不会有任何困难。

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. EnumerateLines将成为TextReader使用迭代器块的扩展方法。 (This means you can also use it for LINQ etc easily.) (这意味着您也可以轻松地将它用于 LINQ 等。)

Or you could use ReadAllLines , to simplify your code:或者您可以使用ReadAllLines来简化您的代码:

http://msdn.microsoft.com/en-us/library/s2tte0y1.aspx 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.通过这种方式,您可以让 .NET 负责所有 EOF/EOL 管理,并且您可以专注于您的内容。

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:如果查看 EndToStream 的实现,您会发现它只是检查缓冲区中是否还有数据,如果没有,则检查它是否可以从底层流中读取更多数据:

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.当然,像这样在您的代码中进行转换会使其依赖于 StreamReader 作为您的阅读器的实际类型,这在开始时并不漂亮。

            var arpStream = ExecuteCommandLine(cmd, arg);
            arpStream.ReadLine(); // Read entries
            while (!arpStream.EndOfStream)
            {
                var line1 = arpStream.ReadLine().Trim();
                //   TeststandInt.SendLogPrint(line, true);
            }

Well, StreamReader is a specialisation of TextReader , in the sense that StreamReader inherits from TextReader.嗯, StreamReaderTextReader 的特化,从某种意义上说 StreamReader 继承自 TextReader。 So there shouldn't be a problem.所以应该没有问题。 :) :)

Maybe read it all into a string and then parse it: StreamReader.ReadToEnd()也许将它全部读入一个字符串然后解析它: StreamReader.ReadToEnd()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM