Skip to content

Commit

Permalink
append
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Aug 15, 2023
1 parent 6fb3c83 commit cc722ed
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
13 changes: 13 additions & 0 deletions rust/examples/files/append.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use std::fs::File;
use std::io::Write;

fn main() {
let filename = "data.txt";
let mut fh = match File::options().append(true).open(filename) {
Ok(fh) => fh,
Err(_) => {
File::create(filename).unwrap()
}
};
writeln!(&mut fh, "Hello").unwrap();
}
8 changes: 8 additions & 0 deletions rust/files.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
{i: create}
{i: writeln!}

* [std::fs::File](https://doc.rust-lang.org/std/fs/struct.File.html)
* [std::io::Write](https://doc.rust-lang.org/std/io/trait.Write.html)

![](examples/files/write.rs)

## Rust - read content of a file as a string
Expand Down Expand Up @@ -98,3 +101,8 @@

![](examples/files/open_file_handling.rs)

## Appand to file
{id: append-to-file}

![](examples/files/append.rs)

0 comments on commit cc722ed

Please sign in to comment.