Skip to content

Commit 575229c

Browse files
committed
Added some I/O operations
1 parent b78bf77 commit 575229c

File tree

3 files changed

+99
-18
lines changed

3 files changed

+99
-18
lines changed

fops/src/data.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,82 +5,82 @@ pub fn load_games() -> Vec<Game> {
55
Game {
66
title: String::from("The Legend of Zelda: Breath of the Wild"),
77
year: 2017,
8-
popularity: 1.5,
8+
popularity: 7.5,
99
},
1010
Game {
1111
title: String::from("Half-Life 2"),
1212
year: 2004,
13-
popularity: 1.6,
13+
popularity: 7.6,
1414
},
1515
Game {
1616
title: String::from("The Witcher 3: Wild Hunt"),
1717
year: 2015,
18-
popularity: 1.8,
18+
popularity: 7.8,
1919
},
2020
Game {
2121
title: String::from("Dark Souls"),
2222
year: 2011,
23-
popularity: 1.1,
23+
popularity: 7.1,
2424
},
2525
Game {
2626
title: String::from("God of War"),
2727
year: 2018,
28-
popularity: 1.25,
28+
popularity: 7.25,
2929
},
3030
Game {
3131
title: String::from("Super Mario Odyssey"),
3232
year: 2017,
33-
popularity: 1.9,
33+
popularity: 7.9,
3434
},
3535
Game {
3636
title: String::from("Hades"),
3737
year: 2020,
38-
popularity: 2.8,
38+
popularity: 8.8,
3939
},
4040
Game {
4141
title: String::from("Resident Evil 4"),
4242
year: 2005,
43-
popularity: 0.9,
43+
popularity: 5.9,
4444
},
4545
Game {
4646
title: String::from("Minecraft"),
4747
year: 2009,
48-
popularity: 2.73,
48+
popularity: 8.73,
4949
},
5050
Game {
5151
title: String::from("Overwatch"),
5252
year: 2016,
53-
popularity: 1.25,
53+
popularity: 8.25,
5454
},
5555
Game {
5656
title: String::from("Grand Theft Auto V"),
5757
year: 2013,
58-
popularity: 1.1,
58+
popularity: 6.1,
5959
},
6060
Game {
6161
title: String::from("Animal Crossing: New Horizons"),
6262
year: 2020,
63-
popularity: 1.4,
63+
popularity: 7.4,
6464
},
6565
Game {
6666
title: String::from("Elden Ring"),
6767
year: 2022,
68-
popularity: 2.98,
68+
popularity: 9.98,
6969
},
7070
Game {
7171
title: String::from("Bloodborne"),
7272
year: 2015,
73-
popularity: 1.12,
73+
popularity: 9.12,
7474
},
7575
Game {
7676
title: String::from("Sekiro: Shadows Die Twice"),
7777
year: 2019,
78-
popularity: 0.78,
78+
popularity: 6.78,
7979
},
8080
Game {
8181
title: String::from("Mass Effect 2"),
8282
year: 2010,
83-
popularity: 1.98,
83+
popularity: 7.98,
8484
},
8585
]
8686
}

fops/src/io_ops.rs

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::game::Game;
2-
use std::fs::File;
2+
use std::fs::{File, OpenOptions};
33
use std::io;
4-
use std::io::{BufRead, Write, stdout};
4+
use std::io::{BufRead, BufReader, BufWriter, Write, stdout};
55
/*
66
Bu fonksiyon stdin yardımıyla bilgi okur.
77
Örneğin terminalden oyun bilgisi girilebilir.
@@ -85,3 +85,63 @@ pub fn read_games_to_vec() -> io::Result<Vec<Game>> {
8585

8686
Ok(games)
8787
}
88+
89+
/*
90+
Aşağıdaki fonksiyon yine bir Game listesini alır ancak parametre olarak gelen
91+
dosyaya kaydetmek için, BufWriter kullanılır.
92+
*/
93+
pub fn write_games_buffered(path: &str, games: &[Game]) -> io::Result<()> {
94+
let file = File::create(path)?;
95+
let mut writer = BufWriter::new(file);
96+
for game in games {
97+
writeln!(writer, "{}", game)?;
98+
}
99+
writer.flush()?;
100+
Ok(())
101+
}
102+
103+
/*
104+
Bu fonksiyon path parametresi ile gelen dosya içeriğini okuyup
105+
Game türünden bir vector olarak geriye döndürür.
106+
*/
107+
pub fn read_games_buffered_into_vec(path: &str) -> io::Result<Vec<Game>> {
108+
let file = File::open(path)?;
109+
let reader = BufReader::new(file);
110+
let mut games = Vec::new();
111+
for line in reader.lines() {
112+
let line = line?;
113+
if !line.is_empty() {
114+
let cols: Vec<&str> = line.split('|').collect();
115+
if cols.len() != 3 {
116+
return Err(io::Error::new(
117+
io::ErrorKind::InvalidData,
118+
format!("Beklenmeyen sütun sayısı: `{}`", line),
119+
));
120+
}
121+
let title = cols[0].to_string();
122+
let year = cols[1]
123+
.parse::<u16>()
124+
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
125+
let popularity = cols[2]
126+
.parse::<f32>()
127+
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
128+
129+
games.push(Game {
130+
title,
131+
year,
132+
popularity,
133+
});
134+
}
135+
}
136+
Ok(games)
137+
}
138+
139+
/*
140+
Aşağıdaki fonksiyon ise parametre olarak gelen dosyayı Append modunda açıp
141+
sonuna yine parametre olarak gelen game değişkenini ekler.
142+
*/
143+
pub fn append_game_to_file(path: &str, game: &Game) -> io::Result<()> {
144+
let mut file = OpenOptions::new().append(true).create(true).open(path)?;
145+
writeln!(file, "{}", game)?;
146+
Ok(())
147+
}

fops/src/main.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::data::load_games;
2+
use crate::game::Game;
23
use crate::io_ops::*;
34

45
mod data;
@@ -31,5 +32,25 @@ fn main() -> std::io::Result<()> {
3132
println!("{}", game);
3233
}
3334

35+
let games = load_games();
36+
write_games_buffered("newGames.dat", &games)?;
37+
let games = read_games_from_file()?;
38+
for game in games {
39+
println!("{}", game);
40+
}
41+
42+
println!("\n\n");
43+
let games = read_games_buffered_into_vec("newGames.dat")?;
44+
for game in games {
45+
println!("{}", game);
46+
}
47+
48+
let shogun = Game {
49+
title: "Shogun Shawdown".to_string(),
50+
year: 2024,
51+
popularity: 8.5,
52+
};
53+
append_game_to_file("games.dat", &shogun)?;
54+
3455
Ok(())
3556
}

0 commit comments

Comments
 (0)