Skip to content

Commit aef794d

Browse files
committed
Cleanup routines added for the created files during runs
1 parent 8e7e878 commit aef794d

File tree

6 files changed

+239
-39
lines changed

6 files changed

+239
-39
lines changed

exercises/24_file_io/file_io1.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,40 @@ fn main() {
1212
// TODO : What would be the expected text ?
1313
assert_eq!(, contents);
1414
}
15-
Err(_) => {
16-
panic!("Error reading file.");
15+
Err(err) => {
16+
eprintln!("File read error. {}", err);
1717
}
1818
}
19+
20+
file_cleanup();
1921
}
2022

2123
fn create_required_files() {
2224
let file_path = Path::new(TEST_FILE_NAME);
2325

2426
if !file_path.exists() {
25-
fs::write(file_path, "This is the file content.").unwrap();
26-
println!("File created.");
27+
let file_write_result = fs::write(file_path, "This is the file content.");
28+
if file_write_result.is_ok() {
29+
println!("Successfully wrote to file.");
30+
} else {
31+
panic!("Error writing to file.");
32+
}
33+
} else {
34+
println!("File already exist.");
35+
}
36+
}
37+
38+
fn file_cleanup() {
39+
let file_path = Path::new(TEST_FILE_NAME);
40+
41+
if file_path.exists() {
42+
let remove_status = fs::remove_file(file_path);
43+
if remove_status.is_ok() {
44+
println!("Successfully removed file.");
45+
} else {
46+
panic!("Error deleting file.");
47+
}
48+
} else {
49+
println!("No cleanup necassary since file not exist.");
2750
}
2851
}

exercises/24_file_io/file_io2.rs

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@ use std::fs;
22
use std::io::{BufRead, BufReader, BufWriter, Write};
33
use std::path::Path;
44

5-
const TEST_FILE_NAME: &str = "MultiLineTextFile.txt";
5+
const TEST_INPUT_FILE_NAME: &str = "MultiLineTextFile.txt";
6+
const TEST_OUTPUT_FILE_NAME: &str = "MultiLineOutputFile.txt";
67

78
fn main() {
89
create_required_files();
9-
let input_file = fs::File::open(TEST_FILE_NAME);
10+
let input_file = fs::File::open(TEST_INPUT_FILE_NAME);
1011

1112
if input_file.is_err() {
1213
panic!("Input file open error");
1314
}
1415

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

17-
let output_file = fs::File::create("MultiLineOutputFile.txt");
19+
let output_file = fs::File::create(TEST_OUTPUT_FILE_NAME);
1820

1921
if output_file.is_err() {
2022
eprintln!(
@@ -23,8 +25,7 @@ fn main() {
2325
);
2426
panic!("Output file open error");
2527
}
26-
// TODO : How to create a new BufReader using input file
27-
let buffered_input_file =;
28+
let mut buffered_file_writer = BufWriter::new(output_file.ok().unwrap());
2829

2930
let mut line_number = 1;
3031

@@ -33,26 +34,59 @@ fn main() {
3334
let write_result =
3435
buffered_file_writer.write(format!("Line {} : {}\n", line_number, line).as_bytes());
3536
if write_result.is_err() {
36-
eprintln!("Write result error: {}", write_result.unwrap_err());
37+
eprintln!("Line write error: {}", write_result.unwrap_err());
3738
break;
3839
}
3940
line_number += 1;
4041
} else {
41-
panic!("Write line error");
42+
panic!("Line read error");
4243
}
4344
}
4445

4546
println!("{} : lines processed", line_number - 1);
47+
file_cleanup();
4648
}
4749

4850
fn create_required_files() {
49-
let file_path = Path::new(TEST_FILE_NAME);
51+
let file_path = Path::new(TEST_INPUT_FILE_NAME);
5052

5153
if !file_path.exists() {
5254
let text = "This is the first line of the text.
5355
This is the second line.
5456
And this is the third and the last line.";
55-
fs::write(file_path, text).unwrap();
56-
println!("File created.");
57+
let file_write_result = fs::write(file_path, text);
58+
59+
if file_write_result.is_ok() {
60+
println!("Multi line file created successfully!");
61+
} else {
62+
eprintln!(
63+
"Error creating file : {} , error : {:?}",
64+
file_path.display(),
65+
file_write_result.err()
66+
);
67+
}
68+
}
69+
}
70+
71+
fn file_cleanup() {
72+
let file_names = vec![TEST_INPUT_FILE_NAME, TEST_OUTPUT_FILE_NAME];
73+
74+
for file_name in file_names {
75+
let file_path = Path::new(file_name);
76+
77+
if file_path.exists() {
78+
let remove_status = fs::remove_file(file_path);
79+
if remove_status.is_ok() {
80+
println!("Successfully deleted file {}", file_name);
81+
} else {
82+
eprintln!(
83+
"Error deleting file {}, err : {:?}",
84+
file_name,
85+
remove_status.err()
86+
);
87+
}
88+
} else {
89+
println!("No cleanup necassary since {} not exist.", file_name);
90+
}
5791
}
5892
}

exercises/24_file_io/file_io3.rs

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,19 @@ fn main() {
1313

1414
if let Ok(meta_data) = meta_data_result {
1515
println!("Metadata about the file : {:?}", path_buffer);
16-
println!("File creation time {:?}", meta_data.created().unwrap());
16+
println!("File creation time {:?}", meta_data.created());
1717
println!("File size {}", meta_data.len());
1818
assert_eq!(meta_data.len(), 117);
1919
println!("File permissions {:?}", meta_data.permissions());
2020
assert!(!meta_data.permissions().readonly());
2121
} else {
22-
panic!("Could not get metadata");
22+
eprintln!(
23+
"Could not get metadata. Error: {:?}",
24+
meta_data_result.err()
25+
);
2326
}
27+
28+
file_cleanup();
2429
}
2530

2631
fn create_required_files() {
@@ -29,15 +34,53 @@ fn create_required_files() {
2934
let dir_path = file_path.parent().unwrap();
3035

3136
if !dir_path.exists() {
32-
fs::create_dir(dir_path).unwrap();
33-
println!("Created directory {:?}", dir_path);
37+
let dir_create_result = fs::create_dir(dir_path);
38+
if dir_create_result.is_ok() {
39+
println!("{:?} created", dir_path);
40+
}
3441
}
3542

3643
if !file_path.exists() {
3744
let text = "This is the first line of the text.
3845
This is the second line.
3946
And this is the third and the last line.";
40-
fs::write(file_path, text).unwrap();
41-
println!("File created.");
47+
let file_write_result = fs::write(&file_path, text);
48+
49+
if file_write_result.is_ok() {
50+
println!("Multi line file created successfully!");
51+
} else {
52+
eprintln!(
53+
"Error creating file : {} , error : {:?}",
54+
file_path.display(),
55+
file_write_result.err()
56+
);
57+
}
58+
}
59+
}
60+
61+
fn file_cleanup() {
62+
let mut path_buffer = PathBuf::new();
63+
64+
path_buffer.push("SampleFilesFolder");
65+
path_buffer.push("MultiLineTextFile.txt");
66+
67+
if path_buffer.exists() {
68+
let remove_status = fs::remove_file(&path_buffer);
69+
if remove_status.is_ok() {
70+
println!("Test file deleted.");
71+
} else {
72+
panic!("Error deleting file.");
73+
}
74+
}
75+
76+
path_buffer.pop();
77+
78+
if path_buffer.exists() {
79+
let remove_status = fs::remove_dir(&path_buffer);
80+
if remove_status.is_ok() {
81+
println!("Test directory deleted.");
82+
} else {
83+
panic!("Error deleting directory.");
84+
}
4285
}
4386
}

solutions/24_file_io/file_io1.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,40 @@ fn main() {
1111
Ok(contents) => {
1212
assert_eq!("This is the file content.", contents);
1313
}
14-
Err(_) => {
15-
panic!("Error reading file.");
14+
Err(err) => {
15+
eprintln!("File read error. {}", err);
1616
}
1717
}
18+
19+
file_cleanup();
1820
}
1921

2022
fn create_required_files() {
2123
let file_path = Path::new(TEST_FILE_NAME);
2224

2325
if !file_path.exists() {
24-
fs::write(file_path, "This is the file content.").unwrap();
25-
println!("File created.");
26+
let file_write_result = fs::write(file_path, "This is the file content.");
27+
if file_write_result.is_ok() {
28+
println!("Successfully wrote to file.");
29+
} else {
30+
panic!("Error writing to file.");
31+
}
32+
} else {
33+
println!("File already exist.");
34+
}
35+
}
36+
37+
fn file_cleanup() {
38+
let file_path = Path::new(TEST_FILE_NAME);
39+
40+
if file_path.exists() {
41+
let remove_status = fs::remove_file(file_path);
42+
if remove_status.is_ok() {
43+
println!("Successfully removed file.");
44+
} else {
45+
panic!("Error deleting file.");
46+
}
47+
} else {
48+
println!("No cleanup necassary since file not exist.");
2649
}
2750
}

solutions/24_file_io/file_io2.rs

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@ use std::fs;
22
use std::io::{BufRead, BufReader, BufWriter, Write};
33
use std::path::Path;
44

5-
const TEST_FILE_NAME: &str = "MultiLineTextFile.txt";
5+
const TEST_INPUT_FILE_NAME: &str = "MultiLineTextFile.txt";
6+
const TEST_OUTPUT_FILE_NAME: &str = "MultiLineOutputFile.txt";
67

78
fn main() {
89
create_required_files();
9-
let input_file = fs::File::open(TEST_FILE_NAME);
10+
let input_file = fs::File::open(TEST_INPUT_FILE_NAME);
1011

1112
if input_file.is_err() {
1213
panic!("Input file open error");
1314
}
1415

1516
let buffered_input_file = BufReader::new(input_file.unwrap());
1617

17-
let output_file = fs::File::create("MultiLineOutputFile.txt");
18+
let output_file = fs::File::create(TEST_OUTPUT_FILE_NAME);
1819

1920
if output_file.is_err() {
2021
eprintln!(
@@ -32,26 +33,59 @@ fn main() {
3233
let write_result =
3334
buffered_file_writer.write(format!("Line {} : {}\n", line_number, line).as_bytes());
3435
if write_result.is_err() {
35-
eprintln!("Write result error: {}", write_result.unwrap_err());
36+
eprintln!("Line write error: {}", write_result.unwrap_err());
3637
break;
3738
}
3839
line_number += 1;
3940
} else {
40-
panic!("Write line error");
41+
panic!("Line read error");
4142
}
4243
}
4344

4445
println!("{} : lines processed", line_number - 1);
46+
file_cleanup();
4547
}
4648

4749
fn create_required_files() {
48-
let file_path = Path::new(TEST_FILE_NAME);
50+
let file_path = Path::new(TEST_INPUT_FILE_NAME);
4951

5052
if !file_path.exists() {
5153
let text = "This is the first line of the text.
5254
This is the second line.
5355
And this is the third and the last line.";
54-
fs::write(file_path, text).unwrap();
55-
println!("File created.");
56+
let file_write_result = fs::write(file_path, text);
57+
58+
if file_write_result.is_ok() {
59+
println!("Multi line file created successfully!");
60+
} else {
61+
eprintln!(
62+
"Error creating file : {} , error : {:?}",
63+
file_path.display(),
64+
file_write_result.err()
65+
);
66+
}
67+
}
68+
}
69+
70+
fn file_cleanup() {
71+
let file_names = vec![TEST_INPUT_FILE_NAME, TEST_OUTPUT_FILE_NAME];
72+
73+
for file_name in file_names {
74+
let file_path = Path::new(file_name);
75+
76+
if file_path.exists() {
77+
let remove_status = fs::remove_file(file_path);
78+
if remove_status.is_ok() {
79+
println!("Successfully deleted file {}", file_name);
80+
} else {
81+
eprintln!(
82+
"Error deleting file {}, err : {:?}",
83+
file_name,
84+
remove_status.err()
85+
);
86+
}
87+
} else {
88+
println!("No cleanup necassary since {} not exist.", file_name);
89+
}
5690
}
5791
}

0 commit comments

Comments
 (0)