adding file IO exericses

This commit is contained in:
Adhyan 2025-01-02 10:26:09 -06:00
parent 1aec7c1152
commit b4b5fd90fd
6 changed files with 194 additions and 0 deletions

View file

View file

@ -0,0 +1,57 @@
use std::fs::File;
use std::io::Read;
fn my_file_reader() -> String {
// TODO: Open the file in read-only mode
let mut file = File::???("ferris.txt").unwrap();
let mut content = String::new();
file.read_to_string(&mut content).unwrap();
content
}
fn main() {}
#[cfg(test)]
mod test {
use std::io::prelude::*;
use std::fs::File;
use my_file_reader;
fn ensure_file_exists() {
let path = "ferris.txt";
let msg = "hello from rustling!";
let mut file = File::create(path).unwrap();
file.write_all(msg.as_bytes()).unwrap();
}
#[test]
fn can_you_read_in_rust() {
ensure_file_exists();
let file_contents = my_file_reader();
assert_eq!(file_contents.len(), 20);
}
#[test]
fn can_you_write_in_rust() {
let msg = "hello world!";
// here the user will enter the path of the file
let path = "ferris.txt";
// the user will fill the create method
let mut file = File::create(path).unwrap();
// the unwrap will be missing here
file.write_all(msg.as_bytes()).unwrap();
}
}

View file

@ -0,0 +1,34 @@
use std::fs::File;
use std::io::Write;
fn my_file_writer() {
let path = "ferris.txt";
let msg = "hello from rustling!";
// TODO: Open the file in write-only mode creating it if it doesn't exist
let mut file = File::???(path).unwrap();
file.write_all(msg.as_bytes()).unwrap();
}
fn main() {}
#[cfg(test)]
mod test {
use std::io::prelude::*;
use std::fs::File;
use my_file_writer;
#[test]
fn can_you_write_in_rust() {
my_file_writer();
let mut file = File::open("ferris.txt").unwrap();
let mut content = String::new();
file.read_to_string(&mut content).unwrap();
assert_eq!(content, "hello from rustling!");
}
}

View file

@ -1209,3 +1209,17 @@ name = "as_ref_mut"
dir = "23_conversions"
hint = """
Add `AsRef<str>` or `AsMut<u32>` as a trait bound to the functions."""
[[exercises]]
name = "fileIO1"
dir = "24_fileIO"
hint = """
The `read` function should return a `Result` with the `String` as the success
"""
[[exercises]]
name = "fileIO2"
dir = "24_fileIO"
hint = """
The `create` function will create a file with the given name
"""

View file

@ -0,0 +1,56 @@
use std::fs::File;
use std::io::Read;
fn my_file_reader() -> String {
let mut file = File::open("ferris.txt").unwrap();
let mut content = String::new();
file.read_to_string(&mut content).unwrap();
content
}
fn main() {}
#[cfg(test)]
mod test {
use std::io::prelude::*;
use std::fs::File;
use my_file_reader;
fn ensure_file_exists() {
let path = "ferris.txt";
let msg = "hello from rustling!";
let mut file = File::create(path).unwrap();
file.write_all(msg.as_bytes()).unwrap();
}
#[test]
fn can_you_read_in_rust() {
ensure_file_exists();
let file_contents = my_file_reader();
assert_eq!(file_contents.len(), 20);
}
#[test]
fn can_you_write_in_rust() {
let msg = "hello world!";
// here the user will enter the path of the file
let path = "ferris.txt";
// the user will fill the create method
let mut file = File::create(path).unwrap();
// the unwrap will be missing here
file.write_all(msg.as_bytes()).unwrap();
}
}

View file

@ -0,0 +1,33 @@
use std::fs::File;
use std::io::Write;
fn my_file_writer() {
let path = "ferris.txt";
let msg = "hello from rustling!";
let mut file = File::create(path).unwrap();
file.write_all(msg.as_bytes()).unwrap();
}
fn main() {}
#[cfg(test)]
mod test {
use std::io::prelude::*;
use std::fs::File;
use my_file_writer;
#[test]
fn can_you_write_in_rust() {
my_file_writer();
let mut file = File::open("ferris.txt").unwrap();
let mut content = String::new();
file.read_to_string(&mut content).unwrap();
assert_eq!(content, "hello from rustling!");
}
}