Hello!
I have a problem with writing content to file.
How to clean the old content of the file?
Code:
use std::io::Write;
use std::fs::OpenOptions;
use std::fs;
fn main() {
const PATH: &str = "myfile.txt";
let mut f = OpenOptions::new().read(true).write(true).create(true).truncate(true).append(false).open(PATH).unwrap();
let content = "content".to_string();
f.write_all(&content.as_bytes()).unwrap();
let content = "new content".to_string();
f.write_all(&content.as_bytes()).unwrap();
let file_content = fs::read_to_string(PATH).expect("Should have been able to read the file");
println!("{:?}", file_content);
}
Output log:
"contentnew content"
Expected output log:
"new content"