pub fn write<'a, 'b, F: AsRef<Fd<'b>>>(
fd: F,
buffer: &[u8],
) -> Result<usize, Error>
Expand description
Write a slice of bytes to a file. The number of bytes successfully written is returned.
Examples found in repository?
More examples
examples/notepad.rs (line 7)
5fn main() {
6 let mut buffer = [0; 4096];
7 write(Fd::stdout, "File: ".as_bytes()).unwrap();
8 let path = read(Fd::stdin, &mut buffer).unwrap();
9 let path = std::path::Path::new(std::ffi::OsStr::from_bytes(path.strip_suffix(b"\n").unwrap()));
10 let fd = open(path, open::Flags::CREATE | open::Flags::WRITE_ONLY, mode!(rw_ ___ ___)).unwrap();
11 loop {
12 let data = read(Fd::stdin, &mut buffer).unwrap();
13 if data == b"\x1b\n" { break }
14 if write(&fd, data).unwrap() == 0 {
15 break
16 }
17 }
18}