diff --git a/vlib/os/os.c.v b/vlib/os/os.c.v index 98d2fc37b4..289d732809 100644 --- a/vlib/os/os.c.v +++ b/vlib/os/os.c.v @@ -62,6 +62,12 @@ pub fn read_bytes(path string) ![]u8 { return res } +// write_bytes writes all the `bytes` to `path`. +@[manualfree] +pub fn write_bytes(path string, bytes []u8) ! { + write_file_array(path, bytes)! +} + fn find_cfile_size(fp &C.FILE) !int { // NB: Musl's fseek returns -1 for virtual files, while Glibc's fseek returns 0 cseek := C.fseek(fp, 0, C.SEEK_END) diff --git a/vlib/os/os_test.c.v b/vlib/os/os_test.c.v index 3e03bf33f1..12c54d706f 100644 --- a/vlib/os/os_test.c.v +++ b/vlib/os/os_test.c.v @@ -784,6 +784,15 @@ fn test_write_file_array_bytes() { // eprintln(rarr.str()) } +fn test_write_bytes() { + fpath := './wbytes.bin' + for arr in [[u8(65), 66, 67, 68, 69, 70], [u8(3), 2, 1], []u8{}] { + os.write_bytes(fpath, arr)! + rarr := os.read_bytes(fpath)! + assert arr == rarr + } +} + fn test_write_file_array_structs() { fpath := './astructs.bin' mut arr := []IntPoint{len: maxn}