StreamReader的,C#,偷看 [英] StreamReader, C#, peek

查看:144
本文介绍了StreamReader的,C#,偷看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有过一段时间检查,如果有更多的从简单的文本文件中读取一个StreamReader。它采用PEEK财产。现在的问题是,当我使用偷看的位置发生了变化,althougth不是假设。

 的FileStream m_fsReader =新的FileStream(
                        m_strDataFileName,
                        FileMode.OpenOrCreate,
                        FileAccess.Read,
                        FileShare.ReadWrite);

StreamReader的m_SR =新的StreamReader(m_fsReader);

Console.WriteLine(IfCanRead SR位置+ m_fsReader.Position +
     和长度+ m_fsReader.Length);

如果(m_SR.Peek()==  -  1){
       Console.WriteLine(IfCanRead假2 SR位置+
             m_fsReader.Position +和长度+ m_fsReader.Length);

       返回false;
}
其他 {
       Console.WriteLine(IfCanRead真2 SR位置+
           m_fsReader.Position +和长度+ m_fsReader.Length);

       返回true;
}
 

解决方案

文档表明StreamReader的位置没有改变,但你正在检查的基础流的当前位置,而不是读者本身。我不明白,这保证了底层流的位置保持不变。事实上,我怀疑它只是读取它和缓冲区内,以保持读者的光标在previous位置。这意味着,它并不能保证底层流的位置不变。

  

StreamReader对象的当前位置不是由皮克改变。返回的值是-1,如果没有更多的字符目前已经上市。

I have a StreamReader that once in a while check if it has more to read from a simple text file. It uses peek property. The problem is that when I am using peek the position is changed, althougth not suppose to.

FileStream m_fsReader = new FileStream(
                        m_strDataFileName,
                        FileMode.OpenOrCreate,
                        FileAccess.Read,
                        FileShare.ReadWrite                        );

StreamReader m_SR = new StreamReader(m_fsReader);

Console.WriteLine("IfCanRead SR Position " + m_fsReader.Position +
     " and Length " + m_fsReader.Length);

if (m_SR.Peek() == -1) {
       Console.WriteLine("IfCanRead false 2 SR Position " + 
             m_fsReader.Position  + " and Length " + m_fsReader.Length);

       return false;
}
else {
       Console.WriteLine("IfCanRead true 2 SR Position " + 
           m_fsReader.Position + " and Length " + m_fsReader.Length);

       return true;
}

解决方案

The documentation indicates that the position of the StreamReader is not changed, but you are checking the underlying stream's current position, not that of the reader itself. I don't see that it guarantees that the position of the underlying stream remains the same. In fact, I suspect that it simply reads it and buffers internally to keep the reader's cursor at the previous position. This would mean that it doesn't guarantee that the underlying stream's position is unchanged.

The current position of the StreamReader object is not changed by Peek. The returned value is -1 if no more characters are currently available.

这篇关于StreamReader的,C#,偷看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆