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
/*
* Made by salmmanfred 2021/01/13
* MIT license
*/


use std::fs::File;
use std::io::{prelude::*,BufReader};
use std::path::Path;



pub fn readFile(names: &str) -> String {
    //reads the file from names
    let s = "".to_string();
    let fnm = s+&names;
    let contents = match std::fs::read_to_string(fnm){
        Err(why) => panic!("couldn't read file: {}", why),
        Ok(contents) => contents,
    };
    
    return contents;
}



pub fn writeFile(names:&str, cont:&str) -> i8 {
    // writes to a file
    let s = "".to_string();
    let fnm = s+&names;
    let path = Path::new(&fnm);
    let display = path.display();

    // Open a file in write-only mode, returns `io::Result<File>`
    let mut file = match File::create(&path) {
        Err(why) => panic!("couldn't create {}: {}", display, why),
        Ok(file) => file,
    };

    // Write the `LOREM_IPSUM` string to `file`, returns `io::Result<()>`
    match file.write_all(cont.as_bytes()) {
        Err(why) => panic!("couldn't write to {}: {}", display, why),
        Ok(_) => println!("successfully wrote to {}", display),
    }
    return 1;
    
    

    
    

}
pub fn readFileLines(fnm:&str) -> Vec<String> {
    // reads the file line by line
    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()
}