diff --git a/exercises/24_file_io/SampleFilesFolder/MultiLineTextFile.txt b/exercises/24_file_io/SampleFilesFolder/MultiLineTextFile.txt deleted file mode 100644 index 0d63d006..00000000 --- a/exercises/24_file_io/SampleFilesFolder/MultiLineTextFile.txt +++ /dev/null @@ -1,3 +0,0 @@ -This is the first line of the text. -This is the second line. -And this is the third and the last line. \ No newline at end of file diff --git a/exercises/24_file_io/SampleFilesFolder/SampleTextFile.txt b/exercises/24_file_io/SampleFilesFolder/SampleTextFile.txt deleted file mode 100644 index 029eee94..00000000 --- a/exercises/24_file_io/SampleFilesFolder/SampleTextFile.txt +++ /dev/null @@ -1 +0,0 @@ -This is the file content. \ No newline at end of file diff --git a/exercises/24_file_io/file_io1.rs b/exercises/24_file_io/file_io1.rs index b022ce65..7b0895e0 100644 --- a/exercises/24_file_io/file_io1.rs +++ b/exercises/24_file_io/file_io1.rs @@ -1,14 +1,17 @@ use std::fs; +use std::path::Path; +const TEST_FILE_NAME: &str = "SampleTextFile.txt"; fn main() { - let read_str_result = fs::read_to_string("exercises/24_file_io/SampleFilesFolder/SampleTextFile.txt"); + create_required_files(); + + let read_str_result = fs::read_to_string(TEST_FILE_NAME); match read_str_result { Ok(contents) => { - // TODO : What should the read string would be ? - let expected_string = - assert_eq!(expected_string, contents); + // TODO : What would be the expected text ? + assert_eq!(, contents); } Err(e) => { eprintln!("Error reading file: {}", e); @@ -16,3 +19,14 @@ fn main() { } } } + + +fn create_required_files(){ + let file_path = Path::new(TEST_FILE_NAME); + + if file_path.exists() == false { + fs::write(&file_path, "This is the file content.").unwrap(); + println!("File created."); + } + +} \ No newline at end of file diff --git a/exercises/24_file_io/file_io2.rs b/exercises/24_file_io/file_io2.rs index c594a76f..7f6921d1 100644 --- a/exercises/24_file_io/file_io2.rs +++ b/exercises/24_file_io/file_io2.rs @@ -1,18 +1,22 @@ use std::fs; use std::io::{BufRead, BufReader, BufWriter, Write}; +use std::path::Path; + +const TEST_FILE_NAME: &str = "MultiLineTextFile.txt"; fn main() { - let input_file = fs::File::open("exercises/24_file_io/SampleFilesFolder/MultiLineTextFile.txt"); + create_required_files(); + let input_file = fs::File::open(TEST_FILE_NAME); if input_file.is_err() { eprintln!("Input file open error : {}", input_file.as_ref().unwrap_err()); assert!(false); } - // TODO : How can we create a BufReader using input_file as parameter - let buffered_input_file = ; + // TODO : How to create a new BufReader using input file + let buffered_input_file =; let output_file = fs::File::create("MultiLineOutputFile.txt"); @@ -40,4 +44,17 @@ fn main() { } println!("{} : lines processed", line_number - 1); +} + +fn create_required_files(){ + let file_path = Path::new(TEST_FILE_NAME); + + if file_path.exists() == false { + let text = "This is the first line of the text. + This is the second line. + And this is the third and the last line."; + fs::write(&file_path, text).unwrap(); + println!("File created."); + } + } \ No newline at end of file diff --git a/exercises/24_file_io/file_io3.rs b/exercises/24_file_io/file_io3.rs index 0c47aaa9..052913dc 100644 --- a/exercises/24_file_io/file_io3.rs +++ b/exercises/24_file_io/file_io3.rs @@ -1,22 +1,22 @@ +use std::fs; use std::path::{Path, PathBuf}; fn main() { + create_required_files(); let mut path_buffer = PathBuf::new(); - path_buffer.push("exercises"); - path_buffer.push("24_file_io"); path_buffer.push("SampleFilesFolder"); path_buffer.push("MultiLineTextFile.txt"); - // TODO : How we can get the metadata from path_buffer ? + // TODO : How to get metadata using path_buffer ? let meta_data_result = path_buffer. if let Ok(meta_data) = meta_data_result { println!("Metadata about the file : {:?}", path_buffer); println!("File creation time {:?}", meta_data.created().unwrap()); println!("File size {}", meta_data.len()); - assert_eq!(meta_data.len(), 101); + assert_eq!(meta_data.len(), 117); println!("File permissions {:?}", meta_data.permissions()); assert_eq!(meta_data.permissions().readonly(), false); }else { @@ -27,4 +27,25 @@ fn main() { +} + +fn create_required_files(){ + let file_path = PathBuf::from("SampleFilesFolder/MultiLineTextFile.txt"); + + let dir_path = file_path.parent().unwrap(); + + if dir_path.exists() == false { + fs::create_dir(dir_path).unwrap(); + println!("Created directory {:?}", dir_path); + } + + if file_path.exists() == false { + + let text = "This is the first line of the text. + This is the second line. + And this is the third and the last line."; + fs::write(&file_path, text).unwrap(); + println!("File created."); + } + } \ No newline at end of file diff --git a/solutions/24_file_io/SampleFilesFolder/MultiLineTextFile.txt b/solutions/24_file_io/SampleFilesFolder/MultiLineTextFile.txt deleted file mode 100644 index 0d63d006..00000000 --- a/solutions/24_file_io/SampleFilesFolder/MultiLineTextFile.txt +++ /dev/null @@ -1,3 +0,0 @@ -This is the first line of the text. -This is the second line. -And this is the third and the last line. \ No newline at end of file diff --git a/solutions/24_file_io/SampleFilesFolder/SampleTextFile.txt b/solutions/24_file_io/SampleFilesFolder/SampleTextFile.txt deleted file mode 100644 index 029eee94..00000000 --- a/solutions/24_file_io/SampleFilesFolder/SampleTextFile.txt +++ /dev/null @@ -1 +0,0 @@ -This is the file content. \ No newline at end of file diff --git a/solutions/24_file_io/file_io1.rs b/solutions/24_file_io/file_io1.rs index 2f170e47..9012f54e 100644 --- a/solutions/24_file_io/file_io1.rs +++ b/solutions/24_file_io/file_io1.rs @@ -1,8 +1,12 @@ use std::fs; +use std::path::Path; +const TEST_FILE_NAME: &str = "SampleTextFile.txt"; fn main() { - let read_str_result = fs::read_to_string("solutions/24_file_io/SampleFilesFolder/SampleTextFile.txt"); + create_required_files(); + + let read_str_result = fs::read_to_string(TEST_FILE_NAME); match read_str_result { Ok(contents) => { @@ -15,21 +19,13 @@ fn main() { } } -#[cfg(test)] -mod tests { - use super::*; - #[test] - fn test_working_directory() { - let working_directory_result = std::path::Path::new(".").canonicalize(); +fn create_required_files(){ + let file_path = Path::new(TEST_FILE_NAME); - match working_directory_result { - Ok(working_directory) => { - println!("The working directory is {:?}", working_directory); - } - Err(error) => { - println!("Error: {:?}", error); - } - } + if !file_path.exists() { + fs::write(file_path, "This is the file content.").unwrap(); + println!("File created."); } + } \ No newline at end of file diff --git a/solutions/24_file_io/file_io2.rs b/solutions/24_file_io/file_io2.rs index 49518f30..af7af411 100644 --- a/solutions/24_file_io/file_io2.rs +++ b/solutions/24_file_io/file_io2.rs @@ -1,10 +1,14 @@ use std::fs; use std::io::{BufRead, BufReader, BufWriter, Write}; +use std::path::Path; + +const TEST_FILE_NAME: &str = "MultiLineTextFile.txt"; fn main() { - let input_file = fs::File::open("solutions/24_file_io/SampleFilesFolder/MultiLineTextFile.txt"); + create_required_files(); + let input_file = fs::File::open(TEST_FILE_NAME); if input_file.is_err() { eprintln!("Input file open error : {}", input_file.as_ref().unwrap_err()); @@ -39,4 +43,17 @@ fn main() { } println!("{} : lines processed", line_number - 1); +} + +fn create_required_files(){ + let file_path = Path::new(TEST_FILE_NAME); + + if file_path.exists() == false { + let text = "This is the first line of the text. + This is the second line. + And this is the third and the last line."; + fs::write(&file_path, text).unwrap(); + println!("File created."); + } + } \ No newline at end of file diff --git a/solutions/24_file_io/file_io3.rs b/solutions/24_file_io/file_io3.rs index 73b50704..f4e95d12 100644 --- a/solutions/24_file_io/file_io3.rs +++ b/solutions/24_file_io/file_io3.rs @@ -1,11 +1,11 @@ +use std::fs; use std::path::{Path, PathBuf}; fn main() { + create_required_files(); let mut path_buffer = PathBuf::new(); - path_buffer.push("solutions"); - path_buffer.push("24_file_io"); path_buffer.push("SampleFilesFolder"); path_buffer.push("MultiLineTextFile.txt"); @@ -15,7 +15,7 @@ fn main() { println!("Metadata about the file : {:?}", path_buffer); println!("File creation time {:?}", meta_data.created().unwrap()); println!("File size {}", meta_data.len()); - assert_eq!(meta_data.len(), 101); + assert_eq!(meta_data.len(), 117); println!("File permissions {:?}", meta_data.permissions()); assert_eq!(meta_data.permissions().readonly(), false); }else { @@ -26,4 +26,25 @@ fn main() { +} + +fn create_required_files(){ + let file_path = PathBuf::from("SampleFilesFolder/MultiLineTextFile.txt"); + + let dir_path = file_path.parent().unwrap(); + + if dir_path.exists() == false { + fs::create_dir(dir_path).unwrap(); + println!("Created directory {:?}", dir_path); + } + + if file_path.exists() == false { + + let text = "This is the first line of the text. + This is the second line. + And this is the third and the last line."; + fs::write(&file_path, text).unwrap(); + println!("File created."); + } + } \ No newline at end of file