Repository navigation
Expand file tree
/
Copy pathfile_io.wave
More file actions
89 lines (69 loc) · 1.64 KB
/
Copy pathfile_io.wave
File metadata and controls
89 lines (69 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import("std::fs::file")::{
remove,
open_read,
create,
read_into,
write,
};
import("std::io::fd")::{
io_close,
io_read,
};
fun main() {
var path: str = "/tmp/wave-file-io-example.txt";
var payload: str = "wave-file-io";
remove(path);
// create uses portable default permissions and returns an owned descriptor.
var write_fd: i64 = create(path);
if (write_fd < 0) {
println("create failed: {}", write_fd);
return;
}
io_close(write_fd);
var write_n: i64 = write(
path,
payload as ptr<u8>,
12
);
if (write_n < 0) {
println("write failed: {}", write_n);
remove(path);
return;
}
println("bytes written = {}", write_n);
// read_into requires caller-owned storage and does not allocate.
var read_buf: array<u8, 64>;
var read_n: i64 = read_into(
path,
&read_buf[0],
64
);
if (read_n < 0) {
println("read_into failed: {}", read_n);
remove(path);
return;
}
println("read_into bytes = {}", read_n);
// io_read works with an already-open file descriptor.
var read_fd: i64 = open_read(path);
if (read_fd < 0) {
println("open_read failed: {}", read_fd);
remove(path);
return;
}
var fd_buf: array<u8, 64>;
var fd_n: i64 = io_read(
read_fd,
&fd_buf[0],
64
);
if (fd_n < 0) {
println("io_read failed: {}", fd_n);
io_close(read_fd);
remove(path);
return;
}
println("io_read bytes = {}", fd_n);
io_close(read_fd);
remove(path);
}