473,387 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Saving a file segment

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
Nov 15 '05 #1
3 1837
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.
"Richard Brookes" <di*********@msn.com> wrote in message
news:bj***********@otis.netspace.net.au...
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

Nov 15 '05 #2
Daniel O'Connell <on******@comcast.net> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #3

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Daniel O'Connell <on******@comcast.net> wrote:
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.

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.
See http://www.yoda.arachsys.com/csharp/readbinary.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Ted G | last post by:
Hello, I'm new wiyh PHP and would like to ask, what is the common way to check if directory path e.g. in url and file requested are in proper format? E.g. if I would give my homepage URL in...
0
by: Michael Tognetti via .NET 247 | last post by:
I am building a parser to parse out segments, then fields, from atext file. It is possible that not all of the segments/fieldswill be present in each line. I have successfully parsed thefile, but...
27
by: Madhav | last post by:
Hi all, I did not understand why do the global vars are initialized to NULL where as the block level variables have random values? I know that the C standard requires this as was mentioned in a...
3
by: R. P. | last post by:
Subject: XSLT to transform a flat XML file into a structured text file I have an XML file that lists the PDF file segment names and titles of a larger document and looks something like this: ...
2
by: Michael | last post by:
We have an ASP.NET 2.0 web application running on a Windows 2003 domain controller. Part of that application needs to read and write files from and to a network share ( living on a MAC Xserveraid)...
1
by: nass | last post by:
hello everyone.. i am not sure what is wrong - never liked strings anyway! my problem is that i am trying to save a QCstring in a shared memory segment and i get a segmentation fault.. here is...
4
by: jesjak | last post by:
hi all, can any one give me some explanation of "wat is .bss segment in object file" and what it will contain and difference between data segment and .bss segment.... help me.... thanks, jes
2
by: z1 | last post by:
hi - i downloaded some data from a weather service that is in an xml file. =object(SimpleXMLElement)#9 (4) { =array(2) { =object(SimpleXMLElement)#11 (2) { =string(25) "Daily Maximum Temperature"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.