Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#MemoryStream.Read()总是读取相同的部分_C#_Buffer_Bytearray_Memorystream - Fatal编程技术网

C#MemoryStream.Read()总是读取相同的部分

C#MemoryStream.Read()总是读取相同的部分,c#,buffer,bytearray,memorystream,C#,Buffer,Bytearray,Memorystream,编辑:解决方案在文章的底部 我正在努力读二进制文件。由于我不想依赖于byte[]AllBytes=File.ReadAllBytes(myPath),因为二进制文件可能相当大,所以我希望使用我称之为“缓冲区”的方法在循环中读取相同大小的小部分(与要读取的文件格式非常吻合) 一切都像一个符咒。每次迭代hour并给出偏移量时,我首先将流重置为其原点。将“设置为开始部分”移到我的外观之外,并将偏移量保留为0,这样做了。如果到达流的末尾,Read将返回零。您确定您的内存流具有您期望的内容吗?我尝试了以下

编辑:解决方案在文章的底部

我正在努力读二进制文件。由于我不想依赖于
byte[]AllBytes=File.ReadAllBytes(myPath)
,因为二进制文件可能相当大,所以我希望使用我称之为“缓冲区”的方法在循环中读取相同大小的小部分(与要读取的文件格式非常吻合)


一切都像一个符咒。每次迭代
hour
并给出偏移量时,我首先将流重置为其原点。将“设置为开始部分”移到我的外观之外,并将偏移量保留为0,这样做了。

如果到达流的末尾,Read将返回零。您确定您的内存流具有您期望的内容吗?我尝试了以下方法,效果如预期:

// Create the source of the memory stream.
UInt32[] source = {42, 4711};
List<byte> sourceBuffer = new List<byte>();
Array.ForEach(source, v => sourceBuffer.AddRange(BitConverter.GetBytes(v)));

// Read the stream.
using (MemoryStream contentStream = new MemoryStream(sourceBuffer.ToArray()))
{
    byte[] buffer = new byte[sizeof (UInt32)];
    int t;
    do
    {
        t = contentStream.Read(buffer, 0, buffer.Length);
        if (t > 0)
        {
           UInt32 value = BitConverter.ToUInt32(buffer, 0);
        }
    } while (t > 0);
}
//创建内存流的源。
UInt32[]源={424711};
List sourceBuffer=新列表();
ForEach(source,v=>sourceBuffer.AddRange(BitConverter.GetBytes(v));
//阅读小溪。
使用(MemoryStream contentStream=new MemoryStream(sourceBuffer.ToArray())
{
字节[]缓冲区=新字节[sizeof(UInt32)];
int t;
做
{
t=contentStream.Read(buffer,0,buffer.Length);
如果(t>0)
{
UInt32值=位转换器.ToUInt32(缓冲区,0);
}
}而(t>0);
}

在调试器中,
sizePerHour
的值是多少?在第一次迭代中,我的缓冲区[]仍然充满了
0
s,在第二次迭代中,我得到了一个
ArgumentException
说明数组的偏移量和/或长度超出了有效边界。为什么我的缓冲区[]在第一次迭代后被
0
填满?sizePerHour的值是446404,我的流的大小是74995872。
    public void ReadStream(MemoryStream ContentStream)
    {
        byte[] buffer = new byte[sizePerHour];

        ContentStream.Seek(0, SeekOrigin.Begin); //Added this line
        for (int hours = 0; hours < NumberHours; hours++)
        {                
            int t = ContentStream.Read(buffer, 0, sizePerHour);
            SecondsToAdd = BitConverter.ToUInt32(buffer, 0);

           // further processing of my byte[] buffer
        }
    }
// Create the source of the memory stream.
UInt32[] source = {42, 4711};
List<byte> sourceBuffer = new List<byte>();
Array.ForEach(source, v => sourceBuffer.AddRange(BitConverter.GetBytes(v)));

// Read the stream.
using (MemoryStream contentStream = new MemoryStream(sourceBuffer.ToArray()))
{
    byte[] buffer = new byte[sizeof (UInt32)];
    int t;
    do
    {
        t = contentStream.Read(buffer, 0, buffer.Length);
        if (t > 0)
        {
           UInt32 value = BitConverter.ToUInt32(buffer, 0);
        }
    } while (t > 0);
}