I am having an issue writing to a file where I want to output 16 bytes. I'm converting a program from C into rust on Linux. In my C program I use fwrite function with the second argument (size_t size) as 2. Here is the C function which outputs the file.
// Write binary to path
void WriteBinary(const char *path, const void *data, const unsigned int dataSize)
{
FILE *fp = fopen(path, "wb");
if (!fp)
{
fprintf(stderr, "fopen() failed for '%hn'\n", firstEpromFile);
}
fwrite(data, 2, dataSize, fp);
fclose(fp);
printf("%d bytes written to \"%s\"\n", dataSize, path);
}
I have written the "almost" equivalent in Rust but it appears to be outputing only 8 bit values. I can't figure out where I'm going wrong.
fn main() {
println!("Build LCD control EPROM Data");
let mut first_eprom_file = vec![0u16; SC_ROM1024];
for (i, elem) in first_eprom_file.iter_mut().enumerate() {
*elem = build_data(i as u16);
}
let mut file = File::create("eprom_lcd.bin").unwrap();
file.write_all(unsafe {
std::slice::from_raw_parts(first_eprom_file.as_ptr() as *const u8,
first_eprom_file.len() * size_of::<u16>())
}).unwrap();
println!("{} bytes written to \"eprom_lcd.bin\"", first_eprom_file.len() * size_of::<u16>());
}
I'm fairly certain that the problem is in the file.write_all() with the *const u8
but I'm not 100% sure where it is going wrong. I haven't posted the build_data() but it is just returning 16 bytes based on a simple match statement, and the values are all valid.