accessing part of file

  • Thread starter Thread starter Bob
  • Start date Start date
B

Bob

Hi,
I have a newbie question. Lets say I have a very large file and a key
that says the information I am interested in is from byte 1,000,000 to
byte 1,100,000 in the file. How can I read in the bytes of interest
without reading in the entire file?

Thanks,
Bob
 
Hi Bob,

Open a Stream and set the position to 1,000,000, then simply Stream.Read
100,000 bytes. Something along the line of

byte[] data = new byte[100000];
using(FileStream fs = new FileStream("MyFile", FileMode.Open))
{
fs.Position = 1000000;
fs.Read(data, 0, data.Length);
}
 
Morten said:
Open a Stream and set the position to 1,000,000, then simply Stream.Read
100,000 bytes. Something along the line of

byte[] data = new byte[100000];
using(FileStream fs = new FileStream("MyFile", FileMode.Open))
{
fs.Position = 1000000;
fs.Read(data, 0, data.Length);
}

Remembering, of course, to check the return value of Read in case it
didn't manage to read as much data as you asked for :)

Jon
 
Beautiful, Thanks so much!

Morten said:
Open a Stream and set the position to 1,000,000, then simply Stream.Read
100,000 bytes. Something along the line of
byte[] data = new byte[100000];
using(FileStream fs = new FileStream("MyFile", FileMode.Open))
{
fs.Position = 1000000;
fs.Read(data, 0, data.Length);
}Remembering, of course, to check the return value of Read in case it
didn't manage to read as much data as you asked for :)

Jon
 
Back
Top