working code

This commit is contained in:
Peter Hart 2020-03-08 15:36:57 -04:00
parent 09ffbede24
commit c5dd891290
4 changed files with 39 additions and 2 deletions

6
Cargo.lock generated Normal file
View File

@ -0,0 +1,6 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "wasi-tutorial"
version = "0.1.0"

1
input.txt Normal file
View File

@ -0,0 +1 @@
Hello

1
output.txt Normal file
View File

@ -0,0 +1 @@
Hello

View File

@ -1,3 +1,32 @@
fn main() { use std::env;
println!("Hello, world!"); use std::fs;
use std::io::{Read, Write};
fn process(input_fname: &str, output_fname: &str) -> Result<(), String> {
let mut input_file =
fs::File::open(input_fname).map_err(|err| format!("error opening input: {}", err))?;
let mut contents = Vec::new();
input_file
.read_to_end(&mut contents)
.map_err(|err| format!("read error: {}", err))?;
let mut output_file = fs::File::create(output_fname)
.map_err(|err| format!("error opening output '{}': {}", output_fname, err))?;
output_file
.write_all(&contents)
.map_err(|err| format!("write error: {}", err))
}
fn main() {
let args: Vec<String> = env::args().collect();
let program = args[0].clone();
if args.len() < 3 {
eprintln!("{} <input_file> <output_file>", program);
return;
}
if let Err(err) = process(&args[1], &args[2]) {
eprintln!("{}", err)
}
} }