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
- How to write struct to file in Rust
- How to write buffer to file in Rust
- How to write string to file in Rust
- How to write bytes to file in Rust
- How to write line to file in Rust
- How to write to file in Rust
- How to read JSON file in Rust
- How to read binary file in Rust
- How to read all lines from file in Rust
More of Rust
- How to use regex with bytes in Rust?
- How to use regex to match a double quote in Rust?
- How to replace a capture group using Rust regex?
- How to implement PartialEq for a Rust HashMap?
- How to use regex to match a group in Rust?
- How to get a capture group using Rust regex?
- How to calculate the inverse of a matrix in Rust?
- How to match a string with regex in Rust?
- How to use look behind in regex in Rust?
- How to parse JSON string in Rust?
See more codes...