-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_ifstream.cpp
More file actions
42 lines (37 loc) · 1.03 KB
/
cpp_ifstream.cpp
File metadata and controls
42 lines (37 loc) · 1.03 KB
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
// 1. deklarasi .open()
// 2. mode operasi file -> ios::in
// 3. membaca data dari file >> getlinea
// .close()
#include <iostream>
#include <fstream>
#include <string>
class Produk {
private:
std::string nama;
double harga;
int jumlah;
public:
Produk(std::string nama, double harga, int jumlah) : nama(nama), harga(harga), jumlah(jumlah) {}
Produk() : nama(""), harga(0.0), jumlah(0) {}
static void bacaData(const std::string& filename) {
std::ifstream inFile(filename);
if (inFile.is_open()) {
std::string nama;
double harga;
int jumlah;
std::cout << "membaca data dari file:" << std::endl;
std::cout << inFile << std::endl;
while (inFile >> nama >> harga >> jumlah) {
std::cout << "nama produk: " << nama << ", harga: Rp" << harga << ", jumlah: " << jumlah << std::endl;
}
inFile.close();
} else {
std::cout << "tidak membuka file" << std::endl;
}
}
};
int main() {
std::string filename = "katalog_produk.txt";
Produk::bacaData(filename);
return 0;
}