如何将 C# .NET TextReader 光标重置回起点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/831417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:23:54  来源:igfitidea点击:

How do you reset a C# .NET TextReader cursor back to the start point?

c#.netfile

提问by Brendan

I have a method that takes either a StringReaderinstance (reading from the clipboard) or a StreamReaderinstance (reading from a file) and, at present, casts either one as a TextReaderinstance.

我有一个方法,它接受一个StringReader实例(从剪贴板读取)或一个StreamReader实例(从文件读取),并且目前将其中一个作为TextReader实例进行转换。

I need it to 'pre-read' some of the source input, then reset the cursor back to the start. I do not necessarily have the original filename. How to I do this?

我需要它来“预读”一些源输入,然后将光标重置回开始。我不一定有原始文件名。我该怎么做?

There is mention of the Seekmethod of System.IO.Streambut this is not implemented in TextReader, although it is in StreamReaderthrough the Basestreamproperty. However StringReaderdoes not have a BaseStreamproperty

提到了 的Seek方法,System.IO.Stream但这并没有在 中实现TextReader,尽管它是StreamReader通过Basestream属性实现的。但是StringReader没有BaseStream财产

采纳答案by Jon Skeet

It depends on the TextReader. If it's a StreamReader, you can use:

这取决于TextReader. 如果是StreamReader,则可以使用:

sr.BaseStream.Position = 0;
sr.DiscardBufferedData();

(Assuming the underlying stream is seekable, of course.)

(当然,假设底层流是可查找的。)

Other implementations of TextReadermay not havea concept of "rewinding", in the same way that IEnumerable<T>doesn't. In many ways you can think of TextReaderas a glorified IEnumerable<char>. It has methods to read whole chunks of data at a time, read lines etc, but it's fundamentally a "forward reading" type.

的其他实现TextReader可能不会“倒带”的概念,在以同样的方式IEnumerable<T>却没有。在很多方面你都可以认为TextReader是一种荣耀IEnumerable<char>。它具有一次读取整块数据、读取行等的方法,但它基本上是一种“正向阅读”类型。

EDIT: I don't believe StringReadersupports any sort of rewinding - you'd be better off recreating the StringReaderfrom the original string, if you can. If that's not feasible, you could always create your own TextReaderclass which proxies all the "normal" calls to another StringReader, but recreates that proxy instance when it needs to.

编辑:我不相信StringReader支持任何类型的倒带 - 如果可以的话,最好StringReader从原始字符串重新创建。如果这不可行,您始终可以创建自己的TextReader类,该类将所有“正常”调用StringReader代理到 another ,但在需要时重新创建该代理实例。

回答by billb

Seek and ye shall find.

寻找,你就会找到。

Seek(0, SeekOrigin.Begin);

TextReaderis derived from StreamReader. StreamReadercontains a BaseStreamproperty

TextReader源自StreamReader. StreamReader包含一个BaseStream属性

回答by Marc Gravell

If it is a StreamReader, and ifthat stream supports seeking, then:

如果它是一个StreamReader,而如果该流求支持,则:

        reader.BaseStream.Seek(0, SeekOrigin.Begin);
        reader.DiscardBufferedData();

However, this is not possible on arbitrary TextReaders. You could perhaps read all of it as a string, then you can use StringReaderrepeatedly?

但是,这在任意TextReaders上是不可能的。你也许可以把它全部读成一个字符串,然后你可以StringReader重复使用?

回答by Martijn

I came across your post, and was rather diappointed that this isn't possible. I hacked up something quick which works: just avoid the stringreader altogether:

我看到你的帖子,很失望这是不可能的。我快速编写了一些有效的方法:完全避免使用 stringreader:

Stream stream = new MemoryStream((new System.Text.ASCIIEncoding().GetBytes(mystring)), false);
reader = new StreamReader(stream, new System.Text.ASCIIEncoding());

It's not pretty, and it uses ASCII (which was what I needed anyway). Note that with a different encoding this won't work, as you will seek the n'th byte which doesn't have be equal to the n'th character. If you do need that you could do something like

它不漂亮,它使用 ASCII(无论如何我都需要)。请注意,使用不同的编码这将不起作用,因为您将寻找不等于第 n 个字符的第 n 个字节。如果你确实需要,你可以做类似的事情

Stream stream = new MemoryStream((new System.Text.UTF32Encoding().GetBytes(mystring)), false);
reader = new StreamReader(stream, new System.Text.UTF32Encoding());

as UTF32 is the only fixed length unicode format.

因为 UTF32 是唯一固定长度的 unicode 格式。

You can now do

你现在可以做

reader.BaseStream.Position = wherever; // or wherever * 4 for the UTF32 variety,
                                       // in your case, the beginning of the string,
                                       // which is always 0 obviously
reader.DiscardBufferedData();