rust-fatfs/examples/write.rs

23 lines
658 B
Rust
Raw Normal View History

2017-11-09 03:40:36 +08:00
extern crate fatfs;
use std::fs::OpenOptions;
use std::io::{self, prelude::*};
2017-11-09 03:40:36 +08:00
use fatfs::{FileSystem, FsOptions, BufStream};
fn main() -> io::Result<()> {
2017-11-09 03:40:36 +08:00
let img_file = match OpenOptions::new().read(true).write(true).open("fat.img") {
Ok(file) => file,
Err(err) => {
println!("Failed to open image!");
return Err(err);
2017-11-09 03:40:36 +08:00
}
};
let buf_stream = BufStream::new(img_file);
2017-11-09 03:40:36 +08:00
let options = FsOptions::new().update_accessed_date(true);
let fs = FileSystem::new(buf_stream, options)?;
let mut file = fs.root_dir().create_file("hello.txt")?;
file.write_all(b"Hello World!")?;
Ok(())
2017-11-09 03:40:36 +08:00
}