Solved

FileStream out of bounds or count to large

Posted on 2009-04-07
3
591 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
Comment Utility
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
Comment Utility
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
Comment Utility
What you want is stream.Seek(2); then do your normal read.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

6 Experts available now in Live!

Get 1:1 Help Now