From cecc35033ab2b8ec57d4cffe9b4bec4c50a154e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Harabie=C5=84?= Date: Wed, 8 Nov 2017 20:40:36 +0100 Subject: [PATCH] Add write example. --- examples/write.rs | 22 ++++++++++++++++++++++ src/dir.rs | 2 +- src/file.rs | 2 +- 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 examples/write.rs diff --git a/examples/write.rs b/examples/write.rs new file mode 100644 index 0000000..c157299 --- /dev/null +++ b/examples/write.rs @@ -0,0 +1,22 @@ +extern crate fatfs; + +use std::fs::OpenOptions; +use std::io::prelude::*; +use std::str; + +use fatfs::{FileSystem, FsOptions, BufStream}; + +fn main() { + let img_file = match OpenOptions::new().read(true).write(true).open("fat.img") { + Ok(file) => file, + Err(err) => { + println!("Failed to open image: {}", err); + return; + } + }; + let mut buf_stream = BufStream::new(img_file); + let options = FsOptions::new().update_accessed_date(true); + let fs = FileSystem::new(&mut buf_stream, options).unwrap(); + let mut file = fs.root_dir().create_file("hello.txt").unwrap(); + file.write_all(b"Hello World!").unwrap(); +} diff --git a/src/dir.rs b/src/dir.rs index b19fc97..74c159e 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -126,7 +126,7 @@ impl <'a, 'b> Dir<'a, 'b> { } } - /// Creates new file or opens existing. + /// Creates new file or opens existing without truncating. pub fn create_file(&mut self, path: &str) -> io::Result> { let (name, rest_opt) = split_path(path); let r = self.find_entry(name); diff --git a/src/file.rs b/src/file.rs index 5ef3bb7..498bf34 100644 --- a/src/file.rs +++ b/src/file.rs @@ -43,7 +43,7 @@ impl <'a, 'b> File<'a, 'b> { } } - /// Truncate file to current position. + /// Truncate file in current position. pub fn truncate(&mut self) -> io::Result<()> { match self.entry { Some(ref mut e) => {