-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_ofstream.cpp
More file actions
56 lines (49 loc) · 1.38 KB
/
cpp_ofstream.cpp
File metadata and controls
56 lines (49 loc) · 1.38 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// 1. deklarasi dan membuka file .open()
// 2. mode operasi dari file
// - ios::out (default)
// - ios::app
// - ios::trunc
// - ios::binary
// 3. penulisan data ke file << cout
// 4. menutup .close()
//
// 1. ofstream
// 2. bukan .open()
// 3. tulis data
// 4. close()
#include <iostream>
#include <string>
#include <fstream>
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) {}
void simpanKeFile(const std::string& filename) {
std::ofstream outFile(filename, std::ios::app);
if (outFile.is_open()) {
outFile << "nama_produk: " << nama << " harga: " << harga << " jumlah: " << jumlah << std::endl;
outFile.close();
std::cout << "data produk berhasil di simpan !" << std::endl;
} else {
std::cout << "data gagal disimpan !" << std::endl;
}
}
void informasiProduk() const {
std::cout << "nama: " << nama << std::endl;
std::cout << "harga: " << harga << std::endl;
std::cout << "jumlah barang: " << jumlah << std::endl;
}
};
int main() {
Produk laptop("thinkpad p15", 20000.00, 30);
Produk skinker("extica", 500.00, 40);
std::string filename = "katalog_produk.txt";
laptop.simpanKeFile(filename);
skinker.simpanKeFile(filename);
laptop.informasiProduk();
skinker.informasiProduk();
return 0;
}