FileStream fs = new FileStream(@"C:\test.txt", FileMode.Open, FileAccess.Read, FileShare.Read);            int fieldsNum = Convert.ToInt32(fs.Length / 4);            for (int m = 0; m < fieldsNum-1; m++)
            {
              
                byte[] buffer = new byte[4];
                fs.Read(buffer, m * 4, 4);
                           } fs.Read(buffer, m * 4, 4);这句出错,请问是怎么回事

解决方案 »

  1.   

    FileStream fs = new FileStream(@"C:\test.txt", FileMode.Open, FileAccess.Read, FileShare.Read);是FileMode.OpenRead吗
      

  2.   

    FileStream FileReadStream = new FileStream
              (readFileName, FileMode.Open, FileAccess.Read);
            FileReadStream.Position = position;
      

  3.   

    大侠
    fs.Read(buffer, m * 4, 4);
    看清定义
            //
            // 摘要:
            //     从流中读取字节块并将该数据写入给定缓冲区中。
            //
            // 参数:
            //   array:
            //     此方法返回时包含指定的字节数组,数组中 offset 和 (offset + count - 1) 之间的值被从当前源中读取的字节替换。
            //
            //   offset:
            //     array 中的字节偏移量,将在此处开始读取字节。
            //
            //   count:
            //     最多读取的字节数。
            //
            // 返回结果:
            //     读入缓冲区中的总字节数。如果当前的字节数没有所请求那么多,则总字节数可能小于所请求的字节数;或者如果已到达流的末尾,则为零。
            //
            // 异常:
            //   System.ArgumentNullException:
            //     array 为 null。
            //
            //   System.ArgumentOutOfRangeException:
            //     offset 或 count 为负。
            //
            //   System.NotSupportedException:
            //     流不支持读取。
            //
            //   System.IO.IOException:
            //     发生 I/O 错误。
            //
            //   System.ArgumentException:
            //     offset 和 count 描述 array 中的无效范围。
            //
            //   System.ObjectDisposedException:
            //     在流关闭后调用方法。
            public override int Read(byte[] array, int offset, int count);争取做法是:fs.Read(buffer, 0, 4);
      

  4.   


    正确做法是:fs.Read(buffer, 0, 4);
      

  5.   

    fs.Length 如果不是4的倍数,除4后会四舍五入的,所以最后长度超过总长度。
    另外,要判断fs.Read的返回值看读取到的长度http://msdn.microsoft.com/zh-cn/library/system.io.filestream.read(v=VS.80).aspx
      

  6.   

    FileReadStream.Position = m*4;??????????????
      

  7.   

    FileReadStream.Position = m*4;这不是必须的,你打开文件Read的时候会自动定位在零,Read()后会自动定位到读过的数据后。
    你的错误不在这里请参考4楼和7楼