karthik
karthik

Reputation:

how to read a file

I want to write a function in a way if i call it with an argument of(100) and the path of particular file it will gets the first 100kb data of the file, and when i call it for the second time with 200 it should return the next 200kb of data leaving the first 100. If there is no more left in the file it should return 0;

Thanks

Upvotes: 0

Views: 323

Answers (2)

Matt Dewey
Matt Dewey

Reputation: 137

You may look at the StreamReader Class. It may get you to where you want to go, though I'm not sure of how specifically to break it down into the kb chunks you want.

Upvotes: 0

Jimmy
Jimmy

Reputation: 91502

most of what you want is handled by the System.IO.File and FileStream. If you want that exact function signature.

Step 1) you need to open a file with a particular path. System.IO.File has a few methods for doing this, including Open and OpenRead, as well as ReadAllXXXX, allowing you to access the flie contents in multiple ways. The one you'd probably want is OpenRead, which returns a FileStream object.

Step 2) you need to read a certain number of bytes. Once you have the FileStream from step 1, you should look at the Stream.ReadBytes method. given an array of bytes, it will read a specified number of bytes from the stream into the array

Upvotes: 2

Related Questions