Saving a file segment

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard Brookes

    #1

    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


  • Daniel O'Connell

    #2
    Re: Saving a file segment

    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">Th e Stream to copy data from.</param>
    /// <param name="output">T he Stream to copy data to.</param>
    /// <param name="offset">T he byte offset at which the copy should
    bein.</param>
    /// <param name="count">Th e 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(Str eam 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(offs et,SeekOrigin.B egin);
    //Reads data from the input stream into a byte buffer
    input.Read(buff er,0,count);
    //Write the buffer to the output stream
    output.Write(bu ffer,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" <dic_brookes@ms n.com> wrote in message
    news:bjevev$28c l$1@otis.netspa ce.net.au...[color=blue]
    > 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
    >
    >[/color]


    Comment

    • Jon Skeet

      #3
      Re: Saving a file segment

      Daniel O'Connell <onyxkirx@comca st.net> wrote:[color=blue]
      > This is pretty simple, actually. Basically, you would simply use a method
      > like the following:[/color]

      <snip>

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

      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 - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Daniel O'Connell

        #4
        Re: Saving a file segment


        "Jon Skeet" <skeet@pobox.co m> wrote in message
        news:MPG.19c524 0ba443b03998a4c 3@news.microsof t.com...[color=blue]
        > Daniel O'Connell <onyxkirx@comca st.net> wrote:[color=green]
        > > This is pretty simple, actually. Basically, you would simply use a[/color][/color]
        method[color=blue][color=green]
        > > like the following:[/color]
        >
        > <snip>
        >
        > It's not quite that simple though. Your reading part looks like this:
        >[color=green]
        > > //Reads data from the input stream into a byte buffer
        > > input.Read(buff er,0,count);[/color]
        >
        > 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.
        >[/color]
        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.
        [color=blue]
        > See http://www.yoda.arachsys.com/csharp/readbinary.html
        >
        > --
        > Jon Skeet - <skeet@pobox.co m>
        > http://www.pobox.com/~skeet/
        > If replying to the group, please do not mail me too[/color]


        Comment

        Working...