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 line to file in Rust
- How to write string to file in Rust
- How to write buffer to file in Rust
- How to write bytes to file in Rust
- How to read all lines from file in Rust
- How to read JSON file in Rust
- How to write to file in Rust
- How to read CSV file in Rust
More of Rust
- How to replace a capture group using Rust regex?
- How to calculate the sum of a Rust slice?
- How do I create an array of strings in Rust?
- How to replace all matches using Rust regex?
- How to use regex to match a double quote in Rust?
- Hashshet example in Rust
- How to use regex captures in Rust?
- How to convert JSON to a struct in Rust?
- How to pop an element from a Rust HashMap?
- How to convert a Rust HashMap to a JSON string?
See more codes...