C# streamreader';使用mono的s基流位置

C# streamreader';使用mono的s基流位置,c#,linux,mono,streamreader,C#,Linux,Mono,Streamreader,我试图编写一些简单的代码来索引一些WikipediaXML页面。其思想是通过使用streamreader读入一个字符来获得每个字符的字节偏移量,然后从字节流中保存位置,以便稍后返回该位置 使用仅包含“”的短测试文件感\na\nb“(8字节),每个字符后有新行。然后我尝试在主函数中使用此代码: using System; using System.IO; namespace indexer { class MainClass { public static vo

我试图编写一些简单的代码来索引一些WikipediaXML页面。其思想是通过使用streamreader读入一个字符来获得每个字符的字节偏移量,然后从字节流中保存位置,以便稍后返回该位置

使用仅包含“”的短测试文件感\na\nb“(8字节),每个字符后有新行。然后我尝试在主函数中使用此代码:

using System;
using System.IO;

namespace indexer

{
    class MainClass
    {
        public static void Main(string[] args)
        {
            StreamReader sr = new StreamReader (@"/home/chris/Documents/len.txt");

            Console.Out.WriteLine(" length of file is " + sr.BaseStream.Length + " bytes ");
            sr.Read (); // read first byte. 
            Console.Out.WriteLine(" current position is  " + sr.BaseStream.Position);

            sr.Close ();
        }
    }
}
这将提供以下输出:

length of file is 8 bytes 
current position is  8
位置应该是3,因为它应该只读取第一个字符。如果我再次使用sr.Read(),我会正确获取下一个字符,但位置仍然是8

我是否误解了这应该如何工作,或者我是否发现了某种错误


谢谢。

不,这不是一个bug
StreamReader
使用一个1KB的缓冲区,当您调用
StremReader.Read()
时,缓冲区会被填满

您应该调用
Encoding.GetByteCount()
方法来获取正在读取的字符或字符串中的字节数‏. 当前编码可在
StreamReader.CurrentEncoding
中找到