Skip to content

Commit 2cfeb6d

Browse files
committed
os: add os.write_bytes/2 as a complement to os.read_bytes/1, add test
1 parent 81e84a6 commit 2cfeb6d

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

vlib/os/os.c.v

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ pub fn read_bytes(path string) ![]u8 {
6262
return res
6363
}
6464

65+
// write_bytes writes all the `bytes` to `path`.
66+
@[manualfree]
67+
pub fn write_bytes(path string, bytes []u8) ! {
68+
write_file_array(path, bytes)!
69+
}
70+
6571
fn find_cfile_size(fp &C.FILE) !int {
6672
// NB: Musl's fseek returns -1 for virtual files, while Glibc's fseek returns 0
6773
cseek := C.fseek(fp, 0, C.SEEK_END)

vlib/os/os_test.c.v

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,15 @@ fn test_write_file_array_bytes() {
784784
// eprintln(rarr.str())
785785
}
786786

787+
fn test_write_bytes() {
788+
fpath := './wbytes.bin'
789+
for arr in [[u8(65), 66, 67, 68, 69, 70], [u8(3), 2, 1], []u8{}] {
790+
os.write_bytes(fpath, arr)!
791+
rarr := os.read_bytes(fpath)!
792+
assert arr == rarr
793+
}
794+
}
795+
787796
fn test_write_file_array_structs() {
788797
fpath := './astructs.bin'
789798
mut arr := []IntPoint{len: maxn}

0 commit comments

Comments
 (0)