Solved

FileStream out of bounds or count to large

Posted on 2009-04-07
3
630 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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