4

As an example, when the below code is run, each time the previous test.csv file is overwritten with a new one. How to append to test.csv instead of overwriting it?

extern crate csv;

use std::error::Error;
use std::process;

fn run() -> Result<(), Box<Error>> {
    let file_path = std::path::Path::new("test.csv");
    let mut wtr = csv::Writer::from_path(file_path).unwrap();

    wtr.write_record(&["City", "State", "Population", "Latitude", "Longitude"])?;
    wtr.write_record(&["Davidsons Landing", "AK", "", "65.2419444", "-165.2716667"])?;
    wtr.write_record(&["Kenai", "AK", "7610", "60.5544444", "-151.2583333"])?;
    wtr.write_record(&["Oakman", "AL", "", "33.7133333", "-87.3886111"])?;

    wtr.flush()?;
    Ok(())
}

fn main() {
    if let Err(err) = run() {
        println!("{}", err);
        process::exit(1);
    }
}

Will the append solution work if the file does not yet exist?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Greg
  • 8,175
  • 16
  • 72
  • 125
  • 2
    Possible duplicate of [What is the best variant for appending a new line in a text file?](https://stackoverflow.com/questions/30684624/what-is-the-best-variant-for-appending-a-new-line-in-a-text-file) – Tim Diekmann May 25 '18 at 00:32
  • 4
    How does a `csv` file differs from any other file? – Tim Diekmann May 25 '18 at 00:49

1 Answers1

11

The csv crate provides Writer::from_writer so you can use anything, which implements Write. When using File, this answer from What is the best variant for appending a new line in a text file? shows a solution:

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

let mut file = OpenOptions::new()
    .write(true)
    .append(true)
    .open("test.csv")
    .unwrap();
let mut wtr = csv::Writer::from_writer(file);

Will the append solution work if the file does not yet exist?

Just add create(true) to the OpenOptions:

let mut file = OpenOptions::new()
    .write(true)
    .create(true)
    .append(true)
    .open("test.csv")
    .unwrap();
let mut wtr = csv::Writer::from_writer(file);
Tim Diekmann
  • 7,755
  • 11
  • 41
  • 69