Saving a file segment

  • Thread starter Thread starter Richard Brookes
  • Start date Start date
R

Richard Brookes

Hi there

i need to take a segment of a mp3 file

say it has 1000 bytes
i want bytes 100-300
and save it as a new file.
can anyone help?

cheers
richard
 
This is pretty simple, actually. Basically, you would simply use a method
like the following:
/// <summary>
/// Copies a a section of the data in input into output.
/// </summary>
/// <param name="input">The Stream to copy data from.</param>
/// <param name="output">The Stream to copy data to.</param>
/// <param name="offset">The byte offset at which the copy should
bein.</param>
/// <param name="count">The number of bytes to copy</param>
/// <remarks>
/// This method does not close either stream, that is left up to the caller.
/// It also does not re-seek the stream, that too is left to the caller
/// </remarks>
void CopySegment(Stream input, Stream output, long offset, int count)
{
//allocates a new buffer to store the data between the streams
byte[] buffer = new byte[count];
//moves the input stream's Position to the offset you wish to read from
input.Seek(offset,SeekOrigin.Begin);
//Reads data from the input stream into a byte buffer
input.Read(buffer,0,count);
//Write the buffer to the output stream
output.Write(buffer,0,count);
//Flush the stream to disk
output.Flush();
}
you must simply pass in the stream of the file you are reading from (input),
the stream to the file or memory buffer you want to write to (output), the
offset of the data in the input stream, and the number of bytes you want to
transfer.
Also, in production code, you should include try catch blocks to handle
errors, i'm electing to leave it out for clarity here.
 
Daniel O'Connell said:
This is pretty simple, actually. Basically, you would simply use a method
like the following:

<snip>

It's not quite that simple though. Your reading part looks like this:
//Reads data from the input stream into a byte buffer
input.Read(buffer,0,count);

which ignores the fact that Stream.Read doesn't have to actually read
the number of bytes you asked for. Instead, you should use the return
value and loop until either you reach the end of the stream or you have
read all that you want to.

See http://www.yoda.arachsys.com/csharp/readbinary.html
 
Jon Skeet said:
<snip>

It's not quite that simple though. Your reading part looks like this:


which ignores the fact that Stream.Read doesn't have to actually read
the number of bytes you asked for. Instead, you should use the return
value and loop until either you reach the end of the stream or you have
read all that you want to.
Thats why the blurb about error checking, I didn't mean just exceptions
although i think thats all I specified. You aren't even guarenteed to get a
proper read and most of the time I would actually use a BinaryReader in this
case, except for the annoyance with streams being closed.
Other conditions could occur that causes problems that aren't addressed
either. Most troubling and possible being trying to seek on a stream that
doesn't support seeking, however with files that shouldn't be a problem.
Still, I should have been paying more attention and mentioned that (the
evils of utility functions at its finest).
Thanks Jon.
 
Back
Top