Solved

FileStream out of bounds or count to large

Posted on 2009-04-07
3
625 Views
Last Modified: 2013-12-17
Hi,

I need to be able to read a file from a certain starting points. Example... Start at byte offset 2, and then read 400 bytes after that. My code below works fine when I start from offset zero, but if I try anything else like start at offset 2 and read 400 bytes, it throws and exception of out of bounds or count to big.
int ncount = 400;
 
            FileStream fs = File.OpenRead(mfilepath); // opening a file stream object
 
            byte[] data = new byte[ncount]; //creating a byte array with the ncount size
 
	    fs.Read(data, 0, 400); //this works
 
            // this one does not work....  fs.Read(data, 2, 400);

Open in new window

0
Comment
Question by:silentthread2k
3 Comments
 
LVL 6

Accepted Solution

by:
SaedSalman earned 500 total points
ID: 24088993
the offset does not mean from where to start reading:

offset
Type: System..::.Int32
The byte offset in array at which the read bytes will be placed.

you will not be able to put 400 bytes in the thired index(offset=2, zero based) of the a byte array. the thired index will holds as long as ONE byte only.
0
 
LVL 11

Expert Comment

by:b_levitt
ID: 24090217
Yes, Saed has identified the problem.

I beleive the FileStream supports seeking.  If so, the solution is...
fs.Seek(2, SeekOrigin.Begin)
...before you call
fs.Read(data, 0, 400);
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 24090279
What you want is stream.Seek(2); then do your normal read.
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question