Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

FileStream out of bounds or count to large

Posted on 2009-04-07
3
Medium Priority
?
647 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 6

Accepted Solution

by:
SaedSalman earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

609 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