Skip to content

Commit 8e7e878

Browse files
committed
Changes in solutions are reflected to the exercises
1 parent c7fd0e4 commit 8e7e878

File tree

3 files changed

+32
-43
lines changed

3 files changed

+32
-43
lines changed

exercises/24_file_io/file_io1.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,17 @@ fn main() {
1212
// TODO : What would be the expected text ?
1313
assert_eq!(, contents);
1414
}
15-
Err(e) => {
16-
eprintln!("Error reading file: {}", e);
17-
assert!(false);
15+
Err(_) => {
16+
panic!("Error reading file.");
1817
}
1918
}
2019
}
2120

2221
fn create_required_files() {
2322
let file_path = Path::new(TEST_FILE_NAME);
2423

25-
if file_path.exists() == false {
26-
fs::write(&file_path, "This is the file content.").unwrap();
24+
if !file_path.exists() {
25+
fs::write(file_path, "This is the file content.").unwrap();
2726
println!("File created.");
2827
}
2928
}

exercises/24_file_io/file_io2.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,58 @@
1-
21
use std::fs;
32
use std::io::{BufRead, BufReader, BufWriter, Write};
43
use std::path::Path;
54

65
const TEST_FILE_NAME: &str = "MultiLineTextFile.txt";
76

87
fn main() {
9-
108
create_required_files();
119
let input_file = fs::File::open(TEST_FILE_NAME);
1210

1311
if input_file.is_err() {
14-
eprintln!("Input file open error : {}", input_file.as_ref().unwrap_err());
15-
assert!(false);
12+
panic!("Input file open error");
1613
}
1714

18-
// TODO : How to create a new BufReader using input file
19-
let buffered_input_file =;
15+
let buffered_input_file = BufReader::new(input_file.unwrap());
2016

2117
let output_file = fs::File::create("MultiLineOutputFile.txt");
2218

2319
if output_file.is_err() {
24-
eprintln!("Output file open error : {}", output_file.as_ref().unwrap_err());
25-
assert!(false);
20+
eprintln!(
21+
"Output file open error : {}",
22+
output_file.as_ref().unwrap_err()
23+
);
24+
panic!("Output file open error");
2625
}
27-
let mut buffered_file_writer = BufWriter::new(output_file.ok().unwrap());
26+
// TODO : How to create a new BufReader using input file
27+
let buffered_input_file =;
2828

2929
let mut line_number = 1;
30-
let mut lines = buffered_input_file.lines();
31-
while let Some(line) = lines.next() {
30+
31+
for line in buffered_input_file.lines() {
3232
if let Ok(line) = line {
33-
let write_result = buffered_file_writer.write(format!("Line {} : {}\n", line_number, line).as_bytes());
33+
let write_result =
34+
buffered_file_writer.write(format!("Line {} : {}\n", line_number, line).as_bytes());
3435
if write_result.is_err() {
3536
eprintln!("Write result error: {}", write_result.unwrap_err());
3637
break;
3738
}
3839
line_number += 1;
39-
}else {
40-
eprintln!("Write line error : {}", line_number);
41-
assert!(false);
40+
} else {
41+
panic!("Write line error");
4242
}
43-
4443
}
4544

4645
println!("{} : lines processed", line_number - 1);
4746
}
4847

49-
fn create_required_files(){
48+
fn create_required_files() {
5049
let file_path = Path::new(TEST_FILE_NAME);
5150

52-
if file_path.exists() == false {
51+
if !file_path.exists() {
5352
let text = "This is the first line of the text.
5453
This is the second line.
5554
And this is the third and the last line.";
56-
fs::write(&file_path, text).unwrap();
55+
fs::write(file_path, text).unwrap();
5756
println!("File created.");
5857
}
59-
60-
}
58+
}

exercises/24_file_io/file_io3.rs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use std::fs;
2-
use std::path::{Path, PathBuf};
2+
use std::path::PathBuf;
33

44
fn main() {
5-
65
create_required_files();
76
let mut path_buffer = PathBuf::new();
87

@@ -18,34 +17,27 @@ fn main() {
1817
println!("File size {}", meta_data.len());
1918
assert_eq!(meta_data.len(), 117);
2019
println!("File permissions {:?}", meta_data.permissions());
21-
assert_eq!(meta_data.permissions().readonly(), false);
22-
}else {
23-
println!("Could not get metadata");
24-
assert!(false);
20+
assert!(!meta_data.permissions().readonly());
21+
} else {
22+
panic!("Could not get metadata");
2523
}
26-
27-
28-
29-
3024
}
3125

32-
fn create_required_files(){
26+
fn create_required_files() {
3327
let file_path = PathBuf::from("SampleFilesFolder/MultiLineTextFile.txt");
3428

3529
let dir_path = file_path.parent().unwrap();
3630

37-
if dir_path.exists() == false {
31+
if !dir_path.exists() {
3832
fs::create_dir(dir_path).unwrap();
3933
println!("Created directory {:?}", dir_path);
4034
}
4135

42-
if file_path.exists() == false {
43-
36+
if !file_path.exists() {
4437
let text = "This is the first line of the text.
4538
This is the second line.
4639
And this is the third and the last line.";
47-
fs::write(&file_path, text).unwrap();
40+
fs::write(file_path, text).unwrap();
4841
println!("File created.");
4942
}
50-
51-
}
43+
}

0 commit comments

Comments
 (0)