rustHow to append to file in Rust
Appending to a file in Rust is a relatively straightforward process. To do so, you need to open the file in write-only mode, seek to the end of the file, and then write the data you want to append. To open the file in write-only mode, you can use the OpenOptions::append
method. To seek to the end of the file, you can use the seek
method. Finally, to write the data, you can use the write
method.
use std::fs::OpenOptions;
use std::io::{Seek, SeekFrom, Write};
fn main() {
let mut file = OpenOptions::new()
.write(true)
.append(true)
.open("my_file.txt")
.unwrap();
file.seek(SeekFrom::End(0)).unwrap();
file.write(b"This is the data I want to append.").unwrap();
}
The above ## Code example opens the file my_file.txt
in write-only mode and appends the data This is the data I want to append.
to the end of the file.
Helpful links
Related
More of Rust
- How to get a capture group using Rust regex?
- How to use non-capturing groups in Rust regex?
- How to replace a capture group using Rust regex?
- How to borrow in loop in Rust
- How to match whitespace with a regex in Rust?
- How to use Unicode in a regex in Rust?
- How to match a URL with a regex in Rust?
- How to replace all matches using Rust regex?
- How to replace strings using Rust regex?
- How to split a string with Rust regex?
See more codes...