Skip to content

Commit 00a18df

Browse files
committed
Add simple IO read write
1 parent b390d36 commit 00a18df

File tree

4 files changed

+137
-4
lines changed

4 files changed

+137
-4
lines changed

fops/src/data.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
use crate::game::Game;
2+
3+
pub fn load_games() -> Vec<Game> {
4+
vec![
5+
Game {
6+
title: String::from("The Legend of Zelda: Breath of the Wild"),
7+
year: 2017,
8+
popularity: 1.5,
9+
},
10+
Game {
11+
title: String::from("Half-Life 2"),
12+
year: 2004,
13+
popularity: 1.6,
14+
},
15+
Game {
16+
title: String::from("The Witcher 3: Wild Hunt"),
17+
year: 2015,
18+
popularity: 1.8,
19+
},
20+
Game {
21+
title: String::from("Dark Souls"),
22+
year: 2011,
23+
popularity: 1.1,
24+
},
25+
Game {
26+
title: String::from("God of War"),
27+
year: 2018,
28+
popularity: 1.25,
29+
},
30+
Game {
31+
title: String::from("Super Mario Odyssey"),
32+
year: 2017,
33+
popularity: 1.9,
34+
},
35+
Game {
36+
title: String::from("Hades"),
37+
year: 2020,
38+
popularity: 2.8,
39+
},
40+
Game {
41+
title: String::from("Resident Evil 4"),
42+
year: 2005,
43+
popularity: 0.9,
44+
},
45+
Game {
46+
title: String::from("Minecraft"),
47+
year: 2009,
48+
popularity: 2.73,
49+
},
50+
Game {
51+
title: String::from("Overwatch"),
52+
year: 2016,
53+
popularity: 1.25,
54+
},
55+
Game {
56+
title: String::from("Grand Theft Auto V"),
57+
year: 2013,
58+
popularity: 1.1,
59+
},
60+
Game {
61+
title: String::from("Animal Crossing: New Horizons"),
62+
year: 2020,
63+
popularity: 1.4,
64+
},
65+
Game {
66+
title: String::from("Elden Ring"),
67+
year: 2022,
68+
popularity: 2.98,
69+
},
70+
Game {
71+
title: String::from("Bloodborne"),
72+
year: 2015,
73+
popularity: 1.12,
74+
},
75+
Game {
76+
title: String::from("Sekiro: Shadows Die Twice"),
77+
year: 2019,
78+
popularity: 0.78,
79+
},
80+
Game {
81+
title: String::from("Mass Effect 2"),
82+
year: 2010,
83+
popularity: 1.98,
84+
},
85+
]
86+
}

fops/src/game.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
use std::fmt::Display;
2+
13
#[derive(Debug)]
24
pub struct Game {
3-
title: String,
4-
release_year: u16,
5-
genre: String,
5+
pub title: String,
6+
pub year: u16,
7+
pub popularity: f32,
8+
}
9+
10+
impl Display for Game {
11+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
12+
write!(f, "{}|{}|{}", self.title, self.year, self.popularity)
13+
}
614
}

fops/src/io_ops.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
use crate::game::Game;
2+
use std::fs::File;
13
use std::io;
2-
use std::io::{Write, stdout};
4+
use std::io::{BufRead, Write, stdout};
35
/*
46
Bu fonksiyon stdin yardımıyla bilgi okur.
57
Örneğin terminalden oyun bilgisi girilebilir.
@@ -21,3 +23,31 @@ pub fn print_games_to_stdout(games: &[String]) {
2123

2224
stdout().write_all(games.join("\n").as_bytes()).unwrap();
2325
}
26+
27+
/*
28+
Bu metot games.dat isimli bir dosyaya, parametre olarak gelen oyunlara ait bilgileri
29+
yazar.
30+
*/
31+
pub fn write_games_to_file(games: &[Game]) -> io::Result<()> {
32+
let mut contents = String::new();
33+
for g in games {
34+
contents.push_str(&g.to_string());
35+
contents.push_str("\n");
36+
}
37+
let mut f = File::create("games.dat")?;
38+
f.write_all(contents.as_bytes())?;
39+
Ok(())
40+
}
41+
42+
/*
43+
Bu metot games.dat dosyasındaki satırları okuyup geriye String türünden değerler içeren
44+
bir vektör döndürür.
45+
*/
46+
pub fn read_games_from_file() -> io::Result<Vec<String>> {
47+
let mut games = Vec::new();
48+
let f = File::open("games.dat")?;
49+
for line in io::BufReader::new(f).lines() {
50+
games.push(line?);
51+
}
52+
Ok(games)
53+
}

fops/src/main.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
use crate::data::load_games;
12
use crate::io_ops::*;
23

4+
mod data;
35
mod game;
46
mod io_ops;
57

@@ -15,5 +17,12 @@ fn main() -> std::io::Result<()> {
1517
classic_games.push(new_game);
1618
print_games_to_stdout(&classic_games);
1719

20+
let games = load_games();
21+
write_games_to_file(&games)?;
22+
let games = read_games_from_file()?;
23+
for game in games {
24+
println!("{}", game);
25+
}
26+
1827
Ok(())
1928
}

0 commit comments

Comments
 (0)