FileStream reading data from a large file. The file size is larger than int. How to set the offset?

FileStream.Read () is defined as:

public override int Read(
    byte[] array,
    int offset,
    int count
)

How can I read a few bytes from an offset greater than int.MaxValue?

Let's say I have a very large file and I want to read 100 MB, starting at position 3147483648.

How can i do this?

+5
source share
2 answers

offsethere is the offset in the array from which to start writing. In your case, just install:

stream.Position = 3147483648;

and then use Read(). offsetmost commonly used when you know that you need to read [n] bytes:

int toRead = 20, bytesRead;
while(toRead > 0 && (bytesRead = stream.Read(buffer, offset, toRead)) > 0)
{
    toRead -= bytesRead;
    offset += bytesRead;
}
if(toRead > 0) throw new EndOfStreamException();

20 buffer ( ). , Read() , , .

+11

http://msdn.microsoft.com/en-us/library/system.io.filestream.read.aspx offset - offset inside the byte[] array:

    : System.Byte []      , ( + - 1) , .

    : System.Int32      , .

    : System.Int32      .

Read() , long Read() . http://msdn.microsoft.com/en-us/library/system.io.filestream.position.aspx

+1

All Articles