Anda di halaman 1dari 12

BAB 10

OPERASI FILE

Program 10.1

//* ------------------------------------------------------*
//* file1.cpp *
//* *
//* contoh untuk merekam string ke file *
//* ------------------------------------------------------*

#include <iostream.h>
#include <fstream.h>
#include <conio.h>

void main()
{
ofstream file_keluaran; // Definisi obyek
file_keluaran.open ("PELANGI.TXT"); // Buka file
cout << "Sedang merekam…" << endl;
file_keluaran << "Pelangi, pelangi"<< endl;
file_keluaran << "Alangkah indahmu"<< endl;
file_keluaran << "Merah, kuning, hijau"<< endl;
file_keluaran << "Di langit yang biru" << endl;
file_keluaran.close(); // Tutup file
getche();
};

Hasil Program :
Program 10.2

//* ------------------------------------------------------*
//* file2.cpp *
//* contoh untuk merekam string ke file. *
//* (versi 2) *
//* ------------------------------------------------------*

#include <iostream.h>
#include <fstream.h>
#include <conio.h>

void main()
{
ofstream file_keluaran,open ("PELANGI.TXT");
cout << "Sedang merekam…" << endl;
file_keluaran <<"Pelangi, pelangi"<< endl;
file_keluaran << "Alangkah indahmu"<< endl;
file_keluaran << "Merah, kuning, hijau"<< endl;
file_keluaran << "Di langit yang biru" << endl;
file_keluaran.close(); // Tutup file
getche();
}

Program 21.3

#include <iostream.h>
#include <fstream.h>
#include <conio.h>

void main()
{
clrscr(); // Hapus layar
const int MAKS = 80;
char penyangga [MAKS+1] ;
ifstream file_masukan ("PELANGI.TXT") ;
while (file_masukan) // Baca seluruh data
{
file_masukan.getline(penyangga,MAKS) ;
cout << penyangga << endl ;
}
file_masukan.close();
getche();
}
Program 10.4

#include <iostream.h>
#include <fstream.h>
#include <conio.h>

void main()
{
clrscr() ; // Hapus layer
const int MAKS = 80;
char penyangga [MAKS+1] ;

ifstream file_masukan ("TESFILE.TXT") ;


while(!file_masukan) // Baca seluruh data
{
file_masukan.getline(penyangga,MAKS) ;
cout << penyangga << endl ;
}
file_masukan.close();
getche();
}

Program 10.5

#include <iostream.h>
#include <fstream.h>
#include <conio.h>

void main()
{
ofstream file_keluaran("PELANGI.TXT");
file_keluaran << "Pelukismu agung"<<endl;
file_keluaran << "Siapakah gerangan"<< endl;
file_keluaran << "Pelangi, pelangi"<< endl;
file_keluaran << "Ciptaan Tuhan" << endl;
file_keluaran.close();
getche(); // Tutup file
}
Program 10.6

#include <iostream.h>
#include <fstream.h>
#include <conio.h>
void main ()
{
ifstream file_masukan ("Z : 123456.789") ;
cout << "good() = " << file_masukan.good()<< endl;
cout << "bad () = " << file_masukan.bad()<< endl;
cout << "fail() = " << file_masukan.fail()<< endl;
cout << "eof () = " << file_masukan.eof()<< endl;
getche();
}

Program 10.7

//* -------------------------------------------------- *
//* infotek.cpp *
//* *
//* contoh program untuk membaca sebarang *
//* file teks. *
//* -------------------------------------------------- *
#include <iostream.h>
#include <fstream.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

void main ( int argc, char *argv [] )


{
clrscr (); // Hapus layer

const int MAKS = 80;


char penyangga [MAKS+1] ;
char namafile [64] ;

if (argc != 2)
{
cerr<< "pemakaian: infotek namafile" << endl;
exit(1) ;
}
strcpy(namafile, argv [1] ) ;
strupr(namafile) ; // Konversi ke huruf capital
// Buka file
ifstream file_masukan (namafile) ;
if (file_masukan.fail () )
{
cerr<<"File "<<namafile<<"tidak ada"<< endl;
exit(1);
}
while (file_masukan) // Baca seluruh data
{
file_masukan.getline(penyangga, MAKS) ;
if (file_masukan.fail())
{
cerr<<"Gagal membaca.Program dihentikan .."<< endl;
exit(1) ;
}
cout << penyangga << endl;
}
file_masukan.close();
getche();
}

Program 10.8

//* -------------------------------------------------- *
//* dafbil.cpp *
//* *
//* contoh menyimpan bilangan decimal, octal dan *
//* heksadesimal (0 desimal s/d 20 desimal *
//* -------------------------------------------------- *
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <conio.h>

void main ( int argc, char *argv [] )


{
clrscr (); // Hapus layer
ofstream file_dafbil ("DAFBIL.TXT") ;
file_dafbil << " DES OKT HEK\n" << endl;
for (int i = 0; i <=20; i++)
file_dafbil<<dec<< setw(5)<<i<<oct<<setw(5)
<<i<< hex << setw(5)<<i<< endl;
file_dafbil.close();
getche();
}
Program 10.9

//* -------------------------------------------------- *
//* fhonor.cpp *
//* *
//* memformat string ke file *
//* -------------------------------------------------- *

#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <conio.h>

void main()
{
struct
{
char nama [15];
long honor;
}
gaji_honorer[]={{"Edi Sumeno",150000},
{"Abdi Sentanu", 90000},
{"Ria Karunia", 8000},
{"Ali Akbar", 25000},};
clrscr (); // Hapus layer
ofstream fhonor ("HONOR.TXT") ;
fhonor<<setiosflags(ios::left)<<setw(16)
<< "N a m a" << "Honor" << endl;
for(int i = 0; i<4; i++)
fhonor<< setiosflags(ios::left)<< setw(16)
<< gaji_honorer[i].nama
<< resetiosflags (ios::left)
<< setiosflags (ios::right) << setw(8)
<<
gaji_honorer[i].honor << endl;
fhonor.close () ;
getche();
}
Program 10.10
//* -------------------------------------------------- *
//* tuliskar.cpp *
//* *
//* merekam ke file per karakter *
//* -------------------------------------------------- *

#include <iostream.h>
#include <fstream.h>
#include <conio.h>

void main()
{
ofstream fkar("KAR.TXT");
fkar.put('A');
fkar.put('B');
fkar.put('1');
fkar.put('\n'); //newline
fkar.put('2');
fkar.put('3');
fkar.close();
getche();
}

Program 10.11

//* -------------------------------------------------- *
//* tuliskar.cpp *
//* *
//* merekam ke file per karakter *
//* -------------------------------------------------- *
#include <iostream.h>
#include <fstream.h>
#include <conio.h>

void main()
{
char karakter;
clrscr(); // Hapus layar
ifstream fkar ("KAR.TXT") ; // Buka file
while (!fkar.eof()) // baca sampai akhir
file
{
kar.get (karakter);
cout << karakter;

}
fkar.close (); // tutup file
getche();
}

Program 10.13

// * ------------------------------------------------ *
// * rekambuk.cpp *
// * *
// * contoh untuk menyimpan obyek ke file. *
// * ------------------------------------------------- *

#include <iostream.h>
#include <fstream.h>
#include <conio.h>

class Buku
{
private:
char kode[10];
char judul[35];
char pengarang[25];
int jumlah;
public:
void info_buku();
};
void baca_buku (Buku buku);
void main()
{
Buku buku_perpustakaan;
baca_buku (buku_perpustakaan);
}

void Buku::info_buku()
{
cout<<"Kode : " << kode << endl;
cout<<"Judul : " << judul<< endl;
cout<<"Pengarang : " << pengarang << endl;
cout<<"Jumlah : " << jumlah << endl;
cout<<endl;
}
//Untuk merekam data buku ke file
void baca_buku(Buku buku)
{
ifstream file_buku ("BUKU.DAT", ios::app);
cout << "<< Daftar Buku >>" <<endl;
cout << endl;
file_buku.read((char *)&buku, sizeof(buku));
while ( !file_buku.eof() )
{
buku.info_buku();
file_buku.read((char *)&buku, sizeof(buku));
}
file_buku.close(); // Tutup file
getche();
}

Program 10.14

// * ------------------------------------------------- *
// * norep.cpp *
// * *
// * contoh untuk menciptakan file *
// * hanya kalau file belum ada *
// * ------------------------------------------------- *

#include <iostream.h>
#include <fstream.h>
#include <conio.h>

void main ()
{
ofstream file_keluaran("BLANK.TXT", ios::noreplace);
if (file_keluaran.good ())
{
cout << "File berhasil diciptakan" << endl;
file_keluaran.close();
}
else
cout << "File sudah ada."
<< " Tidak bisa menciptakan lagi" << endl;
getche();
}
Program 10.18

// * ------------------------------------------------- *
// * frandom.cpp *
// * *
// * contoh pengaksesan file secara random *
// * ------------------------------------------------- *

#include <iostream.h>
#include <fstream.h>
#include <conio.h>

void main()
{
char karakter;
clrscr(); // Hapus layer
fstream fhuruf("HURUF.DAT", ios::in | ios::out);
// --- Tulis huruf capital
for (char huruf='A'; huruf <= 'Z'; huruf ++)fhuruf
<< huruf;
// --- Baca seluruh huruf yang ada pada file
cout<<"Membaca dari karakter pertama hingga terakhir"
<< endl;
for (char huruf = 'A'; huruf <= 'Z'; huruf ++)
{
fhuruf.seekg(huruf - 'A', ios::beg);
fhuruf.get(karakter);
cout << karakter;
}
cout << endl;
cout << "Membaca karakter pertama dan terakhir" << endl;
fhuruf.seekg(0, ios::beg);
fhuruf.get(karakter);
cout << "Karakter pertama: " << karakter << endl;
fhuruf.seekg(-1, ios::end);
fhuruf.get (karakter);
cout << "Karakter terakhir : " << karakter << endl;
fhuruf.close();
getche();
}
Program 10.19

// * ------------------------------------------------- *
// * ftell.cpp *
// * *
// * Contoh pemindahan posisi penunjuk file dan *
// * dan memantau posisi penunjuk file *
// *-------------------------------------------------- *

#include <iostream.h>
#include <fstream.h>
#include <conio.h>

void main()
{
char karakter;
clrscr(); // hapus layer
fstream fhuruf ("HURUF.DAT", ios::in | ios::out);
cout<<"Posisi penunjuk file setelah file dibuka" <<
endl;
cout << "Masukan : " << fhuruf.tellg() << endl;
cout << "keluaran : " << fhuruf.tellp() << endl;

// --- Tulis huruf capital


for (char huruf ='A'; huruf <= 'Z'; huruf++)
fhuruf << huruf;
cout << "Posisi penunjuk file setelah perekaman" << endl;
cout << "Masukan : " << fhuruf.tellg() << endl;
cout << "keluaran : " << fhuruf.tellp() << endl;

// --- Mengubah isi file

fhuruf.seekp(5);
fhuruf.put('a'); // Rekam
cout << "Posisi setelah seekp(5)" << endl;
cout << "Masukan : " << fhuruf.tellg() << endl;
cout << "keluaran : " << fhuruf.tellp() << endl;

// --- Memindahkan penunjukan file masukan

fhuruf.seekg(5);
cout << "Posisi setelah seekg(5)" << endl;
cout << "Masukan : " << fhuruf.tellg() << endl;
cout << "keluaran : " << fhuruf.tellp() << endl;
fhuruf.close();
getche();
}
Program 10.20

// * ------------------------------------------------- *
// * ukurfile.cpp *
// * *
// * contoh program untuk menampilkan ukuran file *
// * ------------------------------------------------- *

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

void main(int argc, char *argv[])


{
if (argc != 2)
{
cerr << "Pemakaian : ukurfile nama_file" << endl;
exit(1);

// --- Buka secara biner

ifstream file_masukan(argv[1], ios::binary);


if (file_masukan.fail())
{
cerr << "File " << strupr(argv[1])
<< " tak ditemukan" << endl;
exit (1);
}

file_masukan.seekg(0, ios::end); // ke akhir file


long ukuran = file_masukan.tellg();

cout << "Ukuran file = " << ukuran


<< " byte " << endl;

}
getche();
}

Anda mungkin juga menyukai