Last active
August 18, 2017 02:50
-
-
Save alisha17/902af5d034d91ef99ed9e2236d826f17 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[macro_use] | |
extern crate error_chain; | |
extern crate same_file; | |
use same_file::Handle; | |
use std::path::Path; | |
use std::fs::File; | |
use std::io::Read; | |
error_chain! { | |
foreign_links { | |
IOError(::std::io::Error); | |
} | |
} | |
fn run() -> Result<()> { | |
let path_to_read = Path::new("new.txt"); | |
let mut file = File::open(&path_to_read)?; | |
let mut s = String::new(); | |
let stdout_handle = Handle::stdout()?; | |
let handle = Handle::from_path(path_to_read)?; | |
if stdout_handle == handle { | |
eprintln!("You are reading and writing to the same file"); | |
} else { | |
println!("The file contents are: \n {:?}",s.to_uppercase()); | |
} | |
file.read_to_string(&mut s)?; | |
Ok(()) | |
} | |
quick_main!(run); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment