Trying to split a file into arbitrary number of bytes and write that section to disk

⚓ Rust    📅 2026-03-02    👤 surdeus    👁️ 1      

surdeus

Hey everyone,

As the title says, I'm trying to write a program that will allow me to give it any file, read it in as bytes, choose an arbitrary, sequential amount of bytes from anywhere in that file, then take that sequence and write it to disk. The program in the current written form has some old code in it from my work earlier today like the loop in the begin_write_loop() function. I know that doesn't work as is.

For the moment, all I'm trying to do is take the first half of the file from 0 to half the file's length (file length / 2) and I'm having trouble.

First, I'm not even sure what the best way to do this is. The code I have so far is here: Trying to split a file arbitrarily. - Pastebin.com

I'm specifically looking for help with the get_file_slices() and write_file_part() functions. I think if I can get those working the way I want, it'll solve my question.

The current iteration of the get_file_slices() function came primarily from this question on stack overflow: https://stackoverflow.com/questions/75957959/split-file-in-arbitrary-n-byte-slices

I'm a little confused why that answer has a vector of vectors. If I don't try to write the file out and just print the contents of file_slices then I do get a list containing 8 byte sequences of the whole file so it does seem to work to a degree. When I try to then go write the file, the write command needs it to be a &[u8] type which makes sense. I'm having trouble converting a Vec<Vec<u8>> to a &[u8].

I tried to flatten the temp_bytes in write_file_part() and then collect them but I get an error:

a value of type `&[u8]` cannot be built from an iterator over elements of type `&u8`
the trait `FromIterator<&u8>` is not implemented for `&[u8]`

So...it's reading the Vec<Vec<u8>> as a &[u8] already? I feel like that's not correct so I must be misunderstanding the error.

4 posts - 3 participants

Read full topic

🏷️ Rust_feed