问题描述
- 在C#中FileStream.Read循环读取时其参数offest如何设置?
-
使用FileStream.Read 循环读取一个文件
While(true)
{
byte[] buffer=new byte[1024];
Fs.Read(buffer,0,buffer.length);
........
}
在循环的时候,按照代码所写不应该是每次都从0的位置开始读取吗?但是实际操作中,每次都是从=+buffer.length开始读取。怎么实现的?
解决方案
参考MSDNFileStream.Read 方法
// Read the source file into a byte array.
byte[] bytes = new byte[fsSource.Length];
int numBytesToRead = (int)fsSource.Length;
int numBytesRead = 0;
while (numBytesToRead > 0)
{
// Read may return anything from 0 to numBytesToRead.
int n = fsSource.Read(bytes, numBytesRead, numBytesToRead);
// Break when the end of the file is reached.
if (n == 0)
break;
numBytesRead += n;
numBytesToRead -= n;
}
numBytesToRead = bytes.Length;
时间: 2024-10-31 01:53:00