On this occasion, I will try to share about file operations, and a little introduction about classes and objects.
Struct v Class
A few weeks ago, I was taught by Mr. Eding Muh. Saprudin ST who discussed struct. The concept of this class is almost the same as struct, except that in the class the information stored is not only data, but also functions related to the data, stored as one unit.
Below we show a program snippet as a comparison between structs and classes:
1. class
class NilaiMahasiswa{
int nim;
char nama[50];
int uts;
int uas;
int tugas;
};
public:
void inputNilai(){
cout<< "NIM :"; cin >> nim;
cout<< "Nama :"; cin >> nama;//kalian bisa juga menggunakan gets
cout<< "TUGAS :"; cin >> tugas;
cout<< "UTS :"; cin >> uts;
cout<< "UAS :"; cin >> uas;
}
2. struct
struct NilaiMahasiswa{
int nim;
char nama[50];
int uts;
int uas;
int tugas;
};
void inputNilai(NilaiMahasiswa nilai){
cout<< "NIM :"; cin >> nilai.nim;
cout<< "Nama :"; cin >> nilai.nama;//kalian bisa juga menggunakan gets
cout<< "TUGAS :"; cin >> nilai.tugas;
cout<< "UTS :"; cin >> nilai.uts;
cout<< "UAS :"; cin >> nilai.uas;
}
So that's the difference between the two, but in this discussion I will not go into too much depth, because what I want to emphasize here is file operations, which in practice involve classes and objects.
Oh yes, as a note, Mr. Eding Muh. Saprudin does not use Borland as a compiler and editor, but he uses the open source version of g++ because he is a Linux user, so don't be surprised if later we find different syntaxes, so we just need to adjust it, OK!
In previous lessons, the implementation of file operations such as writing, reading, searching, deleting or modifying files, we usually use arrays or structs to process data. But this method makes the data non-persistent, meaning that the data will be lost when the program is turned off/closed or the computer is turned off. Therefore, in order for the data to be persistent, we need to save it in the form of a file. Talking about data persistence, there are actually other ways that we can apply, one of which is using a database, but I will not discuss it here.
3. I/O (Input-Output)
C++ I/O (Input-Output)
Source Code:
/*aplikasi nilai mahasiswa dengan menggunakan operasi file
*(tulis,baca,cari,hapus dan modifikasi)
*@author wawan chahyo nugroho / 12131294
*bundet.com
*I/O
*/
#include <iostream.h>
#include <conio.h>
#include <fstream.h>
//using namespace std; //diborland tidak perlu ini
class NilaiMahasiswa12131294{
int nim12131294;
char nama12131294[50];
int uts12131294;
int uas12131294;
int tugas12131294;
public:
void inputNilai12131294(){
cout<< "NIM :"; cin>> nim12131294;
cout<< "Nama :"; cin>> nama12131294;//kalian bisa menggunakan gets
cout<< "TUGAS:"; cin>> tugas12131294;
cout<< "UTS :"; cin>> uts12131294;
cout<< "UAS :"; cin>> uas12131294;
}
void outputNilai12131294(){
cout<< "NIM :" << nim12131294;
cout<< "\nNama :" << nama12131294;
cout<< "\nTUGAS :" << tugas12131294;
cout<< "\nUTS :" << uts12131294;
cout<< "\nUAS :" << uas12131294;
cout<< "\nNilai Akhir :" << nilaiAkhir();
cout<< "\nNilai Huruf :" << nilaiHuruf() <<endl;
}
float nilaiAkhir(){
return(tugas12131294 + uts12131294 + uas12131294) / 3;
}
char nilaiHuruf(){
float nilai = nilaiAkhir();
if(nilai> 80) return 'A';
else if(nilai > 70) return 'B';
else if(nilai > 60) return 'C';
else if(nilai > 40) return 'D';
else return 'E';
}
int getNim(){
return nim12131294;
}
};
/*
char namaFile[30] = "nilai.dat";
//menulis objek ke dlm file
void simpanData() {
NilaiMahasiswa12131294 nilai;
nilai.inputNilai12131294();
//proses simpan objek ke file
ofstream f;
f.open(namaFile, ios::binary|ios::app);
f.write((char*) &nilai, sizeof(nilai));
f.close();
} */
void main(){
NilaiMahasiswa12131294 nilai;
nilai.inputNilai12131294();
nilai.outputNilai12131294();
getch();
}
2. Write - Read
C++ Write - Read
Source Code:
/*
*aplikasi nilai mahasiswa dengan menggunakan operasi file
*(tulis,baca)
*bundet.com
*Wawan Beneran
*Write - Read
*/
#include <iostream.h>
#include <conio.h>
#include <fstream.h>
//using namespace std; //diborland tidak perlu ini
class NilaiMahasiswa12131294{
int nim12131294;
char nama12131294[50];
int uts12131294;
int uas12131294;
int tugas12131294;
public:
void inputNilai12131294(){
cout<< "NIM :"; cin>> nim12131294;
cout<< "Nama :"; cin>> nama12131294;//kalian bisa menggunakan gets
cout<< "TUGAS:"; cin>> tugas12131294;
cout<< "UTS :"; cin>> uts12131294;
cout<< "UAS :"; cin>> uas12131294;
}
void outputNilai12131294(){
cout<< "NIM :" << nim12131294;
cout<< "\nNama :" << nama12131294;
cout<< "\nTUGAS :" << tugas12131294;
cout<< "\nUTS :" << uts12131294;
cout<< "\nUAS :" << uas12131294;
cout<< "\nNilai Akhir :" << nilaiAkhir();
cout<< "\nNilai Huruf :" << nilaiHuruf() <<endl;
}
float nilaiAkhir(){
return(tugas12131294 + uts12131294 + uas12131294) / 3;
}
char nilaiHuruf(){
float nilai = nilaiAkhir();
if(nilai> 80) return 'A';
else if(nilai > 70) return 'B';
else if(nilai > 60) return 'C';
else if(nilai > 40) return 'D';
else return 'E';
}
int getNim(){
return nim12131294;
}
};
char namaFile[30] = "nilai.dat";
//menulis objek ke dlm file
void simpanData12131294() {
NilaiMahasiswa12131294 nilai;
nilai.inputNilai12131294();
//proses simpan objek ke file
ofstream f;
f.open(namaFile, ios::binary|ios::app);
f.write((char*) &nilai, sizeof(nilai));
f.close();
}
//membaca dari file
void bacaData12131294(){
NilaiMahasiswa12131294 nilai;
ifstream f;
f.open( namaFile, ios::binary );
while (f.read((char*)&nilai, sizeof(nilai))) {
nilai.outputNilai12131294();
}
f.close();
}
void main(){
cout <<"Menulis ke file \n";
simpanData12131294();
cout <<"\nMembaca dari file \n";
bacaData12131294();
getch();
}
3. Search
C++ Search (Search)
Source code:
/*
*aplikasi nilai mahasiswa dengan menggunakan operasi file
*bundet.com
*Wawan Beneran
*Search (Cari)
*/
#include <iostream.h>
#include <conio.h>
#include <fstream.h>
//using namespace std; //diborland tidak perlu ini
class NilaiMahasiswa12131294{
int nim12131294;
char nama12131294[50];
int uts12131294;
int uas12131294;
int tugas12131294;
public:
void inputNilai12131294(){
cout<< "NIM :"; cin>> nim12131294;
cout<< "Nama :"; cin>> nama12131294;//kalian juga bisa menggunakan gets
cout<< "TUGAS:"; cin>> tugas12131294;
cout<< "UTS :"; cin>> uts12131294;
cout<< "UAS :"; cin>> uas12131294;
}
void outputNilai12131294(){
cout<< "NIM :" << nim12131294;
cout<< "\nNama :" << nama12131294;
cout<< "\nTUGAS :" << tugas12131294;
cout<< "\nUTS :" << uts12131294;
cout<< "\nUAS :" << uas12131294;
cout<< "\nNilai Akhir :" << nilaiAkhir();
cout<< "\nNilai Huruf :" << nilaiHuruf() <<endl;
}
float nilaiAkhir(){
return(tugas12131294 + uts12131294 + uas12131294) / 3;
}
char nilaiHuruf(){
float nilai = nilaiAkhir();
if(nilai> 80) return 'A';
else if(nilai > 70) return 'B';
else if(nilai > 60) return 'C';
else if(nilai > 40) return 'D';
else return 'E';
}
int getNim(){
return nim12131294;
}
};
char namaFile[30] = "nilai.dat";
//menulis objek ke dlm file
void simpanData12131294() {
NilaiMahasiswa12131294 nilai;
nilai.inputNilai12131294();
//proses simpan objek ke file
ofstream f;
f.open(namaFile, ios::binary|ios::app);
f.write((char*) &nilai, sizeof(nilai));
f.close();
}
//membaca dari file
void bacaData12131294(){
NilaiMahasiswa12131294 nilai;
ifstream f;
f.open( namaFile, ios::binary );
while (f.read((char*)&nilai, sizeof(nilai))) {
nilai.outputNilai12131294();
}
f.close();
}
void cariData12131294(int nimDicari12131294) {
NilaiMahasiswa12131294 nilai, nilaiKetemu;
int ketemu12131294 = 0; // penanda pencarian ditemukan
ifstream f;
f.open( namaFile, ios::binary );
while(f.read((char*)&nilai, sizeof(nilai))){
//jika nim yang dicari ditemukan
if (nilai.getNim() == nimDicari12131294){
nilaiKetemu = nilai;
ketemu12131294 = 1;
break; // hentikan perulangan
}
}
if (ketemu12131294 == 0) {
cout <<"\nNim " << nimDicari12131294 << " tidak ditemukan \n";
}
else{
cout<<endl<<"Nim yang dicari "<<nimDicari12131294<<" ditemukan !!\n\n";
nilaiKetemu.outputNilai12131294();
}
f.close();
}
void main(){
/* cout <<"Menulis ke file \n";
simpanData12131294();
cout <<"\n======================\n";
*/
int t;
cout <<"Masukan NIM : "; cin >> t;
cout <<"\nMembaca dari file \n";
bacaData12131294();
cout <<"\n======================\n";
cout <<"\nMencari data...\n";
cariData12131294(t);
getch();
}
4. Delete - Update
Now we move on to the data deletion operation. In this operation we need two files (Original and Container), which will later read the original data using a buffer (container file).
Long story short, the nim that you want to search for is first stored in the buffer, while the buffer is used as a reference to search for data, if found, the original data is deleted. Meanwhile, the buffer data can be overwritten to perform the next search and deletion.
C++ Delete - Update
Source code:
/*aplikasi nilai mahasiswa dengan menggunakan operasi file
*bundet.com
*Wawan Beneran
*Delete - Update
*/
#include <iostream.h>
#include <conio.h>
#include <fstream.h>
#include <stdio.h>
//using namespace std; //diborland tidak perlu ini
/*
Program ini tidak asing diantara kalian, apalagi kalo bukan aplikasi nilai Mahasiswa.
Langkah 1 - kita buat dulu class NilaiMahasiswa, kemudian kita definisikan seperti nim, nama, uts, uas dan tugas,
berikutnya operasi-operasi yang berhubungan dengan data tersebut.
*/
class NilaiMahasiswa12131294{
int nim12131294;
char nama12131294[50];
int uts12131294;
int uas12131294;
int tugas12131294;
public:
void inputNilai12131294(){
cout<< "NIM :"; cin>> nim12131294;
cout<< "Nama :"; cin>> nama12131294;//kalian bisa menggunakan gets
cout<< "TUGAS:"; cin>> tugas12131294;
cout<< "UTS :"; cin>> uts12131294;
cout<< "UAS :"; cin>> uas12131294;
}
void outputNilai12131294(){
cout<< "NIM :" << nim12131294;
cout<< "\nNama :" << nama12131294;
cout<< "\nTUGAS :" << tugas12131294;
cout<< "\nUTS :" << uts12131294;
cout<< "\nUAS :" << uas12131294;
cout<< "\nNilai Akhir :" << nilaiAkhir();
cout<< "\nNilai Huruf :" << nilaiHuruf() <<endl;
}
float nilaiAkhir(){
return(tugas12131294 + uts12131294 + uas12131294) / 3;
}
char nilaiHuruf(){
float nilai = nilaiAkhir();
if(nilai> 80) return 'A';
else if(nilai > 70) return 'B';
else if(nilai > 60) return 'C';
else if(nilai > 40) return 'D';
else return 'E';
}
int getNim(){
return nim12131294;
}
};
char namaFile[30] = "nilai.dat";
//menulis objek ke dlm file
void simpanData12131294() {
NilaiMahasiswa12131294 nilai;
nilai.inputNilai12131294();
//proses simpan objek ke file
ofstream f;
f.open(namaFile, ios::binary|ios::app);
f.write((char*) &nilai, sizeof(nilai));
f.close();
}
//membaca dari file
void bacaData12131294(){
NilaiMahasiswa12131294 nilai;
ifstream f;
f.open( namaFile, ios::binary );
while (f.read((char*)&nilai, sizeof(nilai))) {
nilai.outputNilai12131294();
}
f.close();
}
void cariData12131294(int nimDicari12131294) {
NilaiMahasiswa12131294 nilai, nilaiKetemu;
int ketemu12131294 = 0; // penanda pencarian ditemukan
ifstream f;
f.open( namaFile, ios::binary );
while(f.read((char*)&nilai, sizeof(nilai))){
//jika nim yang dicari ditemukan
if (nilai.getNim() == nimDicari12131294){
nilaiKetemu = nilai;
ketemu12131294 = 1;
break; // hentikan perulangan
}
}
if (ketemu12131294 == 0) {
cout <<"\nNim " << nimDicari12131294 << " tidak ditemukan \n";
}
else{
cout <<"Nim yang dicari "<<nimDicari12131294<<"ditemukan \n:";
nilaiKetemu.outputNilai12131294();
}
f.close();
}
//hapus data
void hapusData12131294(int nimDicari12131294){
NilaiMahasiswa12131294 nilai;
ifstream fi;
fi.open(namaFile, ios::binary);
ofstream fo;
fo.open("tmp.dat", ios::out|ios::binary);
int ketemu = 0;
while(fi.read((char*) &nilai, sizeof(nilai))){
if(nilai.getNim() != nimDicari12131294){
fo.write((char*)&nilai, sizeof(nilai));
}
else{
ketemu = 1;
}
}
fi.close();
fo.close();
remove(namaFile);
rename("tmp.dat", namaFile);
if (ketemu ==1) {
cout<<" mahasiswa dengan NIM" << nimDicari12131294 << "telah dihapus!\n";
}
else{
cout<<"mahasiswa dengan NIM" << nimDicari12131294 << "tidak ditemukan!\n";
}
}
//edit(modifikasi) data
void updateData12131294(int nimDicari12131294){
NilaiMahasiswa12131294 nilai;
fstream f; //fstream bisa digunakan untuk baca/tulis langsung
f.open( namaFile, ios::in|ios::out );
while(f.read((char*)&nilai, sizeof(nilai))){
if(nilai.getNim() == nimDicari12131294) {
//tampilkan detail nilai sebelum proses modifikasi
nilai.outputNilai12131294();
//input data baru
cout<<"Silakan masukan data baru" <<endl;
nilai.inputNilai12131294();
//hentikan perulangan
// break;
}
}
f.close();
}
void main(){
int pil;
int data;
cout<<"Pilihan"<<endl;
cout<<"1. Cari"<<endl;
cout<<"2. Update"<<endl;
cout<<"3. Delete"<<endl;
cin>>pil;
switch (pil){
case 1:
cout <<"\nMasukan data : ";cin>>data;
cariData12131294(data);
break;
case 2:
cout<<"\nMasukan data :" ;cin>>data;
updateData12131294(data);
break;
case 3:
cout<<"\nMasukan data :" ;cin>>data;
hapusData12131294(data);
break;
default:
cout<<"\nBaca Data\n";
bacaData12131294();
}
getch();
}
Demo Part 1
Demo Part 2
Reference:
System Programming Practical Quiz. STMIK El Rahma Yogyakarta. By Eding Muh. Saprudin, S.Kom
Question 1
MRIZAL WRC Apr 22, 2018, 16:46:00 The updated data that was commented on and the one that wasn't, when I tried it, it worked(?) What's the difference, min?
Response 1
Hi MRIZAL WRC, there is no difference, bro, maybe at that time I used it for debugging and then when it was not used I forgot to delete it, thank you for your input. I have deleted it so the code is cleaner, thanks
Question 2
Are there any examples of SIM and STNK programs?
Response 2
Hi Noby715, currently there isn't one, but maybe I can make one if the price is right.