Solved

FileStream out of bounds or count to large

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now