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 write to file in Rust
- How to read binary file in Rust
- How to read JSON file in Rust
- How to read file line by line in rust
More of Rust
- How to replace strings using Rust regex?
- How to use captures_iter with regex in Rust?
- How to use regex to match a double quote in Rust?
- How to use regex lookbehind in Rust?
- How to convert struct to bytes in Rust
- How to use 'or' in Rust regex?
- How to insert an element into a Rust HashMap if it does not already exist?
- How to print a Rust HashMap?
- How to loop through a Rust HashMap?
- How to compare two Rust HashMaps?
See more codes...