C
C#3y ago
Surihia

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:
videofile.mp4, 50, pieceName
videofile.mp4, 50, pieceName
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
MODiX
MODiX3y ago
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.
ero
ero3y ago
Hm?
Surihia
SurihiaOP3y ago
umm...which one do I use and can you explain a bit about buffers as I have never used them till now.
ero
ero3y ago
I would assume you just pass the stream and the amount of bytes you want to write
using var source = File.OpenRead(...);
using var dest = File.OpenWrite(...);

source.CopyTo(dest, 50);
using var source = File.OpenRead(...);
using var dest = File.OpenWrite(...);

source.CopyTo(dest, 50);
Surihia
SurihiaOP3y ago
wait is this with filestreams and is the code like limited to file size below 2gb?
ero
ero3y ago
I don't know if streams are limited File.Open_ creates a FileStream, yes
Surihia
SurihiaOP3y ago
nope it still copies all the bytes instead of just 50 of them.
Aaron
Aaron3y ago
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
byte[] buffer = new byte[50];
var bytesRead = sourceStream.Read(buffer);
outputStream.Write(buffer, 0, bytesRead);
byte[] buffer = new byte[50];
var bytesRead = sourceStream.Read(buffer);
outputStream.Write(buffer, 0, bytesRead);
Surihia
SurihiaOP3y ago
Would the position of the sourceStream when the data is read, be from whatever position that is specified in the seek origin method ?
ero
ero3y ago
It would read from whatever the current position in the stream is
Aaron
Aaron3y ago
seek sets the position, and reading also sets the position
Surihia
SurihiaOP3y ago
Do I have to specify more arguements in the sourceStream.Read(buffer); code ?
Aaron
Aaron3y ago
what .net are you targeting
Surihia
SurihiaOP3y ago
Framework 4.7.2
Aaron
Aaron3y ago
then yes
Surihia
SurihiaOP3y ago
is there a different code for my framework version ? oh ok
Aaron
Aaron3y ago
you need var bytesRead = sourceStream.Read(buffer, 0, buffer.Length);
Surihia
SurihiaOP3y ago
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.
Want results from more Discord servers?
Add your server