mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-12-28 06:49:19 +00:00
File IO Exercises added and tested
This commit is contained in:
parent
b5d440fdc3
commit
e916628d84
16
exercises/24_file_io/README.md
Normal file
16
exercises/24_file_io/README.md
Normal file
@ -0,0 +1,16 @@
|
||||
# File IO
|
||||
|
||||
Rust Provides several file I/O functions in the standard library. Buffered reads and writes provides better performance by reducing underlying reads.
|
||||
|
||||
## Further information
|
||||
|
||||
Here is the documentation for these functions in the standard library.
|
||||
|
||||
- [ReadToString](https://doc.rust-lang.org/std/fs/fn.read_to_string.html)
|
||||
- [BufReader](https://doc.rust-lang.org/std/io/struct.BufReader.html)
|
||||
- [BufWriter](https://doc.rust-lang.org/std/io/struct.BufWriter.html)
|
||||
- [Path](https://doc.rust-lang.org/stable/std/path/struct.Path.html)
|
||||
- [PathBuf](https://doc.rust-lang.org/std/path/struct.PathBuf.html)
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,3 @@
|
||||
This is the first line of the text.
|
||||
This is the second line.
|
||||
And this is the third and the last line.
|
||||
@ -0,0 +1 @@
|
||||
This is the file content.
|
||||
18
exercises/24_file_io/file_io1.rs
Normal file
18
exercises/24_file_io/file_io1.rs
Normal file
@ -0,0 +1,18 @@
|
||||
use std::fs;
|
||||
|
||||
fn main() {
|
||||
|
||||
let read_str_result = fs::read_to_string("exercises/24_file_io/SampleFilesFolder/SampleTextFile.txt");
|
||||
|
||||
match read_str_result {
|
||||
Ok(contents) => {
|
||||
// TODO : What should the read string would be ?
|
||||
let expected_string =
|
||||
assert_eq!(expected_string, contents);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Error reading file: {}", e);
|
||||
assert!(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
43
exercises/24_file_io/file_io2.rs
Normal file
43
exercises/24_file_io/file_io2.rs
Normal file
@ -0,0 +1,43 @@
|
||||
|
||||
use std::fs;
|
||||
use std::io::{BufRead, BufReader, BufWriter, Write};
|
||||
|
||||
fn main() {
|
||||
|
||||
let input_file = fs::File::open("exercises/24_file_io/SampleFilesFolder/MultiLineTextFile.txt");
|
||||
|
||||
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 = ;
|
||||
|
||||
let output_file = fs::File::create("MultiLineOutputFile.txt");
|
||||
|
||||
if output_file.is_err() {
|
||||
eprintln!("Output file open error : {}", output_file.as_ref().unwrap_err());
|
||||
assert!(false);
|
||||
}
|
||||
let mut buffered_file_writer = BufWriter::new(output_file.ok().unwrap());
|
||||
|
||||
let mut line_number = 1;
|
||||
let mut lines = buffered_input_file.lines();
|
||||
while let Some(line) = lines.next() {
|
||||
if let Ok(line) = line {
|
||||
let write_result = buffered_file_writer.write(format!("Line {} : {}\n", line_number, line).as_bytes());
|
||||
if write_result.is_err() {
|
||||
eprintln!("Write result error: {}", write_result.unwrap_err());
|
||||
break;
|
||||
}
|
||||
line_number += 1;
|
||||
}else {
|
||||
eprintln!("Write line error : {}", line_number);
|
||||
assert!(false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
println!("{} : lines processed", line_number - 1);
|
||||
}
|
||||
30
exercises/24_file_io/file_io3.rs
Normal file
30
exercises/24_file_io/file_io3.rs
Normal file
@ -0,0 +1,30 @@
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
fn main() {
|
||||
|
||||
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 ?
|
||||
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);
|
||||
println!("File permissions {:?}", meta_data.permissions());
|
||||
assert_eq!(meta_data.permissions().readonly(), false);
|
||||
}else {
|
||||
println!("Could not get metadata");
|
||||
assert!(false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@ -1200,3 +1200,26 @@ name = "as_ref_mut"
|
||||
dir = "23_conversions"
|
||||
hint = """
|
||||
Add `AsRef<str>` or `AsMut<u32>` as a trait bound to the functions."""
|
||||
|
||||
# File IO Exercises
|
||||
|
||||
[[exercises]]
|
||||
name = "file_io1"
|
||||
dir = "24_file_io"
|
||||
hint = """
|
||||
Basic File Reading
|
||||
"""
|
||||
|
||||
[[exercises]]
|
||||
name = "file_io2"
|
||||
dir = "24_file_io"
|
||||
hint = """
|
||||
Buffered Reading & Writing
|
||||
"""
|
||||
|
||||
[[exercises]]
|
||||
name = "file_io3"
|
||||
dir = "24_file_io"
|
||||
hint = """
|
||||
Path Manipulation & Metadata
|
||||
"""
|
||||
|
||||
@ -0,0 +1,3 @@
|
||||
This is the first line of the text.
|
||||
This is the second line.
|
||||
And this is the third and the last line.
|
||||
@ -0,0 +1 @@
|
||||
This is the file content.
|
||||
16
solutions/24_file_io/file_io1.rs
Normal file
16
solutions/24_file_io/file_io1.rs
Normal file
@ -0,0 +1,16 @@
|
||||
use std::fs;
|
||||
|
||||
fn main() {
|
||||
|
||||
let read_str_result = fs::read_to_string("solutions/24_file_io/SampleFilesFolder/SampleTextFile.txt");
|
||||
|
||||
match read_str_result {
|
||||
Ok(contents) => {
|
||||
assert_eq!("This is the file content.", contents);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Error reading file: {}", e);
|
||||
assert!(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
42
solutions/24_file_io/file_io2.rs
Normal file
42
solutions/24_file_io/file_io2.rs
Normal file
@ -0,0 +1,42 @@
|
||||
|
||||
use std::fs;
|
||||
use std::io::{BufRead, BufReader, BufWriter, Write};
|
||||
|
||||
fn main() {
|
||||
|
||||
let input_file = fs::File::open("solutions/24_file_io/SampleFilesFolder/MultiLineTextFile.txt");
|
||||
|
||||
if input_file.is_err() {
|
||||
eprintln!("Input file open error : {}", input_file.as_ref().unwrap_err());
|
||||
assert!(false);
|
||||
}
|
||||
|
||||
let buffered_input_file = BufReader::new(input_file.unwrap());
|
||||
|
||||
let output_file = fs::File::create("MultiLineOutputFile.txt");
|
||||
|
||||
if output_file.is_err() {
|
||||
eprintln!("Output file open error : {}", output_file.as_ref().unwrap_err());
|
||||
assert!(false);
|
||||
}
|
||||
let mut buffered_file_writer = BufWriter::new(output_file.ok().unwrap());
|
||||
|
||||
let mut line_number = 1;
|
||||
let mut lines = buffered_input_file.lines();
|
||||
while let Some(line) = lines.next() {
|
||||
if let Ok(line) = line {
|
||||
let write_result = buffered_file_writer.write(format!("Line {} : {}\n", line_number, line).as_bytes());
|
||||
if write_result.is_err() {
|
||||
eprintln!("Write result error: {}", write_result.unwrap_err());
|
||||
break;
|
||||
}
|
||||
line_number += 1;
|
||||
}else {
|
||||
eprintln!("Write line error : {}", line_number);
|
||||
assert!(false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
println!("{} : lines processed", line_number - 1);
|
||||
}
|
||||
29
solutions/24_file_io/file_io3.rs
Normal file
29
solutions/24_file_io/file_io3.rs
Normal file
@ -0,0 +1,29 @@
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
fn main() {
|
||||
|
||||
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");
|
||||
|
||||
let meta_data_result = path_buffer.metadata();
|
||||
|
||||
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);
|
||||
println!("File permissions {:?}", meta_data.permissions());
|
||||
assert_eq!(meta_data.permissions().readonly(), false);
|
||||
}else {
|
||||
println!("Could not get metadata");
|
||||
assert!(false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user