41

I am using this code to append a new line to the end of a file:

let text = "New line".to_string();

let mut option = OpenOptions::new();
option.read(true);
option.write(true);
option.create(true);

match option.open("foo.txt") {
    Err(e) => {
        println!("Error");
    }
    Ok(mut f) => {
        println!("File opened");
        let size = f.seek(SeekFrom::End(0)).unwrap();
        let n_text = match size {
            0 => text.clone(),
            _ => format!("\n{}", text),
        };
        match f.write_all(n_text.as_bytes()) {
            Err(e) => {
                println!("Write error");
            }
            Ok(_) => {
                println!("Write success");
            }
        }

        f.sync_all();
    }
}

It works, but I think it's too difficult. I found option.append(true);, but if I use it instead of option.write(true); I get "Write error".

Shepmaster
  • 326,504
  • 69
  • 892
  • 1,159
Aleksandr
  • 1,395
  • 2
  • 11
  • 8
  • 4
    Unrelated to the question itself, but `.to_owned()` is faster than `.to_string()`, and is the preferred alternative unless you want to stringify some Show-implementing type. – llogiq Jun 06 '15 at 16:24
  • 1
    Thanks. I will use it. – Aleksandr Jun 07 '15 at 15:19
  • 3
    Since [specialize ToString for str](https://github.com/rust-lang/rust/pull/32586), `.to_string()` is as fast as `.to_owned()`. – AurevoirXavier Sep 06 '18 at 10:32

1 Answers1

72

Using OpenOptions::append is the clearest way to append to a file:

use std::fs::OpenOptions;
use std::io::prelude::*;

fn main() {
    let mut file = OpenOptions::new()
        .write(true)
        .append(true)
        .open("my-file")
        .unwrap();

    if let Err(e) = writeln!(file, "A new line!") {
        eprintln!("Couldn't write to file: {}", e);
    }
}

As of Rust 1.8.0 (commit) and RFC 1252, append(true) implies write(true). This should not be a problem anymore.

Before Rust 1.8.0, you must use both write and append — the first one allows you to write bytes into a file, the second specifies where the bytes will be written.

Shepmaster
  • 326,504
  • 69
  • 892
  • 1,159