Skip to content

Instantly share code, notes, and snippets.

@andrew-d
Last active December 25, 2015 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrew-d/22c630cacb5a1326eff0 to your computer and use it in GitHub Desktop.
Save andrew-d/22c630cacb5a1326eff0 to your computer and use it in GitHub Desktop.
extern mod extra;
use std::{os, str, vec};
use std::path::Path;
use std::rt::io;
use std::rt::io::support::PathLike;
use std::rt::io::support;
use std::rt::io::file;
use std::rt::io::{Reader, Writer};
use std::rt::io::file::{FileInfo, FileReader};
use extra::sort;
fn main() {
let args = os::args();
let inpath = &Path::new(args[1].clone());
let outpath = &Path::new(args[2].clone());
if !inpath.exists() {
fail!("input file doesn't exist");
}
let mut infile = inpath.open_reader(io::Open).expect("can't open file");
let size = file::stat(inpath).expect("can't stat").size;
println!("Size of file is: {}", size);
let mut buff = vec::from_elem(size as uint, 0 as u8);
let read_str = match infile.read(buff).unwrap() {
-1 | 0 => fail!("error reading"),
n => str::from_utf8(buff.slice_to(n))
};
let mut lines : ~[&str] = read_str.trim().split_iter('\n').collect();
sort::quick_sort3(lines);
let mut outfile = outpath.open_writer(io::Create).expect("can't open output");
let outbuff = lines.connect("\n");
outfile.write(outbuff.as_bytes());
println!("Done!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment