PC Review


Reply
Thread Tools Rate Thread

accessing part of file

 
 
Bob
Guest
Posts: n/a
 
      23rd Jan 2007
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

 
Reply With Quote
 
 
 
 
Morten Wennevik [C# MVP]
Guest
Posts: n/a
 
      23rd Jan 2007
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);
}


On Tue, 23 Jan 2007 07:40:50 +0100, Bob <(E-Mail Removed)> wrote:

> 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
>




--
Happy Coding!
Morten Wennevik [C# MVP]
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      23rd Jan 2007
Morten Wennevik [C# MVP] wrote:
> 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

 
Reply With Quote
 
Bob
Guest
Posts: n/a
 
      24th Jan 2007
Beautiful, Thanks so much!

On Jan 23, 2:21 am, "Jon Skeet [C# MVP]" <s...@pobox.com> wrote:
> Morten Wennevik [C# MVP] wrote:
>
> > 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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Medical CDs - [Part 2], [Part 1], [Part 3 = MEDLINE 1986-1998] CDs ola Windows XP Basics 0 18th Aug 2007 08:19 AM
Accessing Web Part personalization info from a different Web page Michael Microsoft ASP .NET 0 9th Dec 2005 04:58 AM
Accessing COM+ Component(Part of different Domain envn) from ASPX page Kiran Nakhate Microsoft ASP .NET 1 25th Jan 2005 04:25 PM
accessing a site part 2 anonymous Windows XP Networking 0 30th Mar 2004 04:18 AM
Re: Accessing part from main switch board Charles Frankert Microsoft Access Form Coding 0 2nd Jul 2003 05:52 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:07 PM.