use std::fs::File;
use std::io::{prelude::*,BufReader};
use std::path::Path;
use std::fs;
pub fn remove_file(fnm:&str) -> std::io::Result<()> {
fs::remove_file(fnm)?;
Ok(())
}
pub fn read_file(names: &str) -> String {
let s = "".to_string();
let fnm = s+&names;
let contents = std::fs::read_to_string(fnm).unwrap();
return contents;
}
pub fn write_file(names:&str, cont:&str) -> std::io::Result<()> {
let s = "".to_string();
let fnm = s+&names;
let path = Path::new(&fnm);
let mut file = File::create(&path)?;
file.write_all(cont.as_bytes())?;
Ok(())
}
pub fn read_file_lines(fnm:&str) -> Vec<String> {
let x: &str = &fnm;
let mut errcode = "Error".to_owned();
errcode.push_str(x);
let file = File::open(x).expect(&errcode);
let buf = BufReader::new(file);
buf.lines()
.map(|l| l.expect("Could not parse line"))
.collect()
}
pub fn write_file_bytes(names:&str, cont:Vec<u8>) -> std::io::Result<()> {
let s = "".to_string();
let fnm = s+&names;
let path = Path::new(&fnm);
let mut file = File::create(&path)?;
for x in cont{
file.write(&[x])?;
}
Ok(())
}
pub fn read_file_bytes(filename: &str) -> Vec<u8> {
let mut f = File::open(&filename).expect("no file found");
let metadata = fs::metadata(&filename).expect("unable to read metadata");
let mut buffer = vec![0; metadata.len() as usize];
f.read(&mut buffer).expect("buffer overflow");
buffer
}