-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaesar_cipher.rs
68 lines (56 loc) · 1.84 KB
/
caesar_cipher.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::env;
use std::fs::{File, OpenOptions};
use std::io::{BufReader, BufWriter, Read, Write};
const BUF_SIZE: usize = 4096;
fn caesar_cipher_file(input_path: &str, output_path: &str, shift: u32) -> std::io::Result<()> {
// Open input file with buffered reader
let input_file = File::open(input_path)?;
let mut reader = BufReader::new(input_file);
// Open output file with buffered writer (truncate existing or create new)
let output_file = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(output_path)?;
let mut writer = BufWriter::new(output_file);
let mut buffer = [0u8; BUF_SIZE];
loop {
// Read chunk from input file
let bytes_read = reader.read(&mut buffer)?;
if bytes_read == 0 {
break; // End of file
}
// Process each byte in the chunk
for byte in &mut buffer[..bytes_read] {
*byte = ((u32::from(*byte) + shift) % 256) as u8;
}
// Write processed chunk to output file
writer.write_all(&buffer[..bytes_read])?;
}
// Ensure all data is flushed to disk
writer.flush()?;
Ok(())
}
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() != 4 {
eprintln!("Usage: {} <input> <output> <shift>", args[0]);
std::process::exit(1);
}
// Parse shift value
let shift = match args[3].parse::<u32>() {
Ok(n) => n,
Err(_) => {
eprintln!("Error: Shift must be a positive integer");
std::process::exit(1);
}
};
// Process files
match caesar_cipher_file(&args[1], &args[2], shift) {
Ok(_) => println!("File processed successfully"),
Err(e) => {
eprintln!("Error processing files: {}", e);
std::process::exit(1);
}
}
}