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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
}