开发者

Reading data from a particular location of a FileStream using .NET

I am trying to read data from a file stream as shown below:

fileStream.Read(byteArray, offset, length);

The problem is that my offset and length are Unsigned I开发者_StackOverflow中文版nts and above function accepts only ints. If I typecast to int, I am getting a negative value for offset which is meaningless and not acceptable by the function.

The offset and length are originally taken from another byte array as shown below:

BitConverter.ToUInt32(length, 0); //length is a 4 byte long byte-array

What is the right way to read from arbitrary locations of a file stream.


I am not sure if this is the best way to handle it, but you can change the position of the stream and use offset 0. The Position is of type long.

fileStream.Position = (long)length;
fileStream.Read(byteArray, 0, sizeToRead);


For such a filesize you should read your file in small blocks, proccess the block and read the next. int.MaxValue is about ~2GB, uint.MaxValue ~4GB. Such a size doesn't fit in most computers ram ;)


if you are having problems with conversion, something similar might help:

    uint myUInt;
    int i = (int)myUInt; or
    int i = Convert.ToInt32(myUInt);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜