Add write example.

This commit is contained in:
Rafał Harabień 2017-11-08 20:40:36 +01:00
parent d5c37f2af5
commit cecc35033a
3 changed files with 24 additions and 2 deletions

22
examples/write.rs Normal file
View File

@ -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();
}

View File

@ -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<File<'a, 'b>> { pub fn create_file(&mut self, path: &str) -> io::Result<File<'a, 'b>> {
let (name, rest_opt) = split_path(path); let (name, rest_opt) = split_path(path);
let r = self.find_entry(name); let r = self.find_entry(name);

View File

@ -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<()> { pub fn truncate(&mut self) -> io::Result<()> {
match self.entry { match self.entry {
Some(ref mut e) => { Some(ref mut e) => {