Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.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# 读取将溢出缓冲区错误_C#_Filestream_Buffer Overrun - Fatal编程技术网

C# 读取将溢出缓冲区错误

C# 读取将溢出缓冲区错误,c#,filestream,buffer-overrun,C#,Filestream,Buffer Overrun,我不知道我做错了什么。我的代码在第一次运行时按预期工作,但在第二次运行时它不会运行,并抛出一个异常,该异常只是说“读取将超出缓冲区%” 我推测我的缓冲区有问题,它不喜欢我覆盖它 我有基本相同的代码,并将其更改为与msdn示例匹配,但没有乐趣 using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read)) { byte[] buffer = new byte[1024]; int bytes

我不知道我做错了什么。我的代码在第一次运行时按预期工作,但在第二次运行时它不会运行,并抛出一个异常,该异常只是说“读取将超出缓冲区%”

我推测我的缓冲区有问题,它不喜欢我覆盖它

我有基本相同的代码,并将其更改为与msdn示例匹配,但没有乐趣

using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
    byte[] buffer = new byte[1024];

    int bytesRead = 0;
    long bytesToRead = stream.Length;

    while (bytesToRead > 0)
    {               
        int n = stream.Read (buffer, bytesRead, 1024);
        if (n == 0) break;

        //do work on buffer...

        bytesRead += n;
        bytesToRead -= n;
    }
}   

在.Net 2.0 Mono中工作。

-第二个参数在缓冲区中是偏移量,在您的情况下应为0。

-第二个参数在缓冲区中是偏移量,在您的情况下应为0。

-第二个参数在缓冲区中是偏移量,在您的情况下应为0案例。

读取参数用于缓冲区信息,您将数据从一个偏移量存储到另一个偏移量+长度 在文件集中前进
stream.Position=字节读取

读取参数用于缓冲区信息,您将数据从一个偏移量存储到另一个偏移量+n长度 在文件集中前进
stream.Position=字节读取

读取参数用于缓冲区信息,您将数据从一个偏移量存储到另一个偏移量+n长度 在文件集中前进
stream.Position=字节读取

读取参数用于缓冲区信息,您将数据从一个偏移量存储到另一个偏移量+n长度 在文件集中前进
stream.Position=字节读取

我认为bytesRead变量应该保持在零。这是缓冲区中的偏移量,而不是文件。我认为ByteRead变量应该保持为零。这是缓冲区中的偏移量,而不是文件。我认为ByteRead变量应该保持为零。这是缓冲区中的偏移量,而不是文件。我认为ByteRead变量应该保持为零。这是缓冲区中的偏移量,而不是文件。