Creating a file with a fixed size
I am writing a video splitter app for my personal storage and would like some help.
What my app does is split a video file into multiple tiny files. I will call each tiny file a piece here.
The issue I am facing is creating a new file piece with a custom size. I want to specify a size value for a piece and that size value is given via a argument.
Here is a rough example of my argument:
I can create a new file with a filestream, and open the video in another filestream. but the fs.CopyTo() method doesn't have a parameter to copy an x amount of bytes and so I can't tell it to copy 50 bytes of data from one filestream to another filestream.
How do I handle this problem ?
18 Replies
❯ Method: System.IO.Stream.CopyTo(Stream)
Reads the bytes from the current stream and writes them to another stream. Both streams positions are advanced by the number of bytes copied.
❯ Method: System.IO.Stream.CopyTo(Stream, Int32)
Reads the bytes from the current stream and writes them to another stream, using a specified buffer size. Both streams positions are advanced by the number of bytes copied.
❯ Method: System.IO.Stream.CopyToAsync(Stream)
Asynchronously reads the bytes from the current stream and writes them to another stream. Both streams positions are advanced by the number of bytes copied.
3/6 results shown ~ Click Here for more results
React with ❌ to remove this embed.
Hm?
umm...which one do I use and can you explain a bit about buffers as I have never used them till now.
I would assume you just pass the stream and the amount of bytes you want to write
wait is this with filestreams and is the code like limited to file size below 2gb?
I don't know if streams are limited
File.Open_ creates a FileStream, yes
nope it still copies all the bytes instead of just 50 of them.
that parameter is the buffer size, not the byte count
as far as I'm aware you have to make your own buffer and copy into and write out of it
so
Would the position of the sourceStream when the data is read, be from whatever position that is specified in the seek origin method ?
It would read from whatever the current position in the stream is
seek sets the position, and reading also sets the position
Do I have to specify more arguements in the sourceStream.Read(buffer); code ?
what .net are you targeting
Framework 4.7.2
then yes
is there a different code for my framework version ?
oh ok
you need
var bytesRead = sourceStream.Read(buffer, 0, buffer.Length);
Getting this exception:
Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.'
nvm it worked
thank you for helping out you both.