1 / 13

Pertemuan 27

Pertemuan 27. OPERASI FILE Bag.2. Dasar Pemrograman Renni Angreni, S.Kom. Operasi Berbasis Karakter. Operasi pada file tidak harus dalam bentuk string, seperti sejumlah contoh program yang telah diberikan .

Télécharger la présentation

Pertemuan 27

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pertemuan27 OPERASI FILE Bag.2 Dasar Pemrograman Renni Angreni, S.Kom.

  2. OperasiBerbasisKarakter • Operasipada file tidakharusdalambentuk string, sepertisejumlahcontoh program yang telahdiberikan. • Kita dapatmelakukanoperasipada file berbasiskarakter. Perekamanke file dilakukankarakterdemikarakterdanbegitu pula prosespembacaannya. • Untukmelakukanini, kitagunakanfungsianggotaput()danget()padakelasofstreamdanifstream. • put()untukmenuliskansebuahkarakterke file. • get()untukmengambilsebuahkarakterdari file.

  3. Contoh #include <iostream> #include <conio.h> #include <string> #include <fstream> using namespace std; void main() { ofstreamfileKeluar("karakter.txt"); fileKeluar.put('A'); fileKeluar.put('B'); fileKeluar.put('C'); fileKeluar.put('\n'); fileKeluar.put('A'); fileKeluar.put('N'); fileKeluar.close(); _getch(); }

  4. Contoh #include <iostream> #include <conio.h> #include <string> #include <fstream> using namespace std; void main() { char karakter; ifstreamfileMasuk("karakter.txt"); while (!fileMasuk.eof()) { fileMasuk>>karakter; if (fileMasuk.fail()) break; cout<<"Karakter yang dibaca : “; cout<<karakter<<endl; } fileMasuk.close(); _getch(); }

  5. Contoh #include <iostream> #include <conio.h> #include <fstream> using namespace std; void main() { char karakter; ifstreamfileMasuk("karakter.txt"); while (!fileMasuk.eof()) { fileMasuk.get(karakter); if (fileMasuk.fail()) break; cout<<karakter; } fileMasuk.close(); _getch(); }

  6. OperasiPenunjuk File • Setiapobjek file memilikipenunjuk file tersendiri. Penunjuk file bertindaksebagaipenunjukposisididalam file terhadapperekamanataupunpembacaan. Dalamhalinisetiapobjek file memilikiduabuahpenunjuk file, yaitupenunjuk file untukkeperluanpembacaan data danpenunjuk file untukkeperluanperekaman data. • Penunjuk file baikuntukkeperluanpembacaanmaupunperekamandapatdipindahkandenganmenggunakanfungsianggotaseekg()danseekp().

  7. seekg()  bergunauntukmemindahkanpenunjuk file yang digunakansebagaipenunjukoperasipembacaan. • seekp() bergunauntukmemindahkanpenunjuk file yang digunakansebagaipenunjukoperasiperekaman. • Sintaxnya : seekp(intposisi);atauseekp(offset, acuan); seekg(intposisi);atauseekg(offset, acuan); • Denganposisiacuan yang dapatkitagunakanantara lain : ios::begposisiawal file ios::curposisi pointer saatini ios::endposisiakhir file

  8. #include <iostream> #include <conio.h> #include <fstream> using namespace std; void main() { char karakter; fstreamfHuruf("huruf.txt", ios::out); fHuruf.close(); fHuruf.open("huruf.txt", ios::in|ios::out); for (char huruf = 'A'; huruf<='Z'; huruf++) fHuruf.put(huruf); cout<<"Membacadariawalhinggaakhir"<<endl; for (char huruf='A'; huruf<='Z'; huruf++) { fHuruf.seekg(huruf-'A', ios::beg); fHuruf.get(karakter); cout<<karakter; } cout<<endl; fHuruf.close(); _getch(); } Contoh

  9. #include <iostream> #include <conio.h> #include <fstream> using namespace std; void main() { char karakter; fstreamfHuruf("huruf.txt", ios::out); fHuruf.close(); fHuruf.open("huruf.txt", ios::in|ios::out); for (char huruf = 'A'; huruf<='Z'; huruf++) fHuruf.put(huruf); cout<<"Membacakarakterpertamadanterakhir"<<endl; fHuruf.seekg(0, ios::beg); fHuruf.get(karakter); cout<<"KarakterPertama : "<<karakter<<endl; fHuruf.seekg(-1, ios::end); fHuruf.get(karakter); cout<<"Karakterterakhir : "<<karakter<<endl; fHuruf.close(); _getch(); } Contoh

  10. Informasimengenaiposisipenunjuk file untukoperasimasukanataupunkeluarandapatdiperolehdenganmenggunakanfungsianggotatellp()dantellg(). • tellp() menunjukkanposisipenunjuk file perekaman. • tellg() menunjukkanposisipenunjuk file pembacaan. • Fungsitellp()dantellg()mengembalikannilai integer yang menyatakanposisi pointer perekamandanpembacaan. • Fungsiinidapatdigunakandenganlangsungmenugaskanfungsiinikedalamsuatuvariabel integer.

  11. #include <iostream> #include <conio.h> #include <fstream> using namespace std; void main() { char karakter; fstreamfHuruf("huruf2.txt", ios::out); fHuruf.close(); fHuruf.open("huruf2.txt", ios::out|ios::in); cout<<"posisipenunjuk file setelah file dibuka"<<endl; cout<<"Masukan : "<<fHuruf.tellg()<<endl; cout<<"Keluaran : "<<fHuruf.tellp()<<endl; for(char huruf='A'; huruf<='Z'; huruf++) fHuruf<<huruf; cout<<"Posisipenunjuk file setelahperekaman"<<endl; cout<<"Masukan : "<<fHuruf.tellg()<<endl; cout<<"Keluaran : "<<fHuruf.tellp()<<endl; fHuruf.close(); _getch(); } Contoh

  12. #include <iostream> #include <conio.h> #include <fstream> using namespace std; void main() { char karakter; fstreamfHuruf("huruf2.txt", ios::out); fHuruf.close(); fHuruf.open("huruf2.txt", ios::out|ios::in); for(char huruf='A'; huruf<='Z'; huruf++) fHuruf<<huruf; fHuruf.seekp(5); fHuruf.put('!'); cout<<"Posisisetelahproses : "<<endl; cout<<"Masukan : "<<fHuruf.tellg()<<endl; cout<<"Keluaran : "<<fHuruf.tellp()<<endl; fHuruf.seekg(5); cout<<"Posisisetelahseekg(5) : "<<endl; cout<<"Masukan : "<<fHuruf.tellg()<<endl; cout<<"Keluaran : "<<fHuruf.tellp()<<endl; fHuruf.seekg(0); for(char huruf='A'; huruf<='Z'; huruf++) { fHuruf>>karakter; cout<<karakter; } fHuruf.close(); _getch(); } Contoh

  13. -- Sekian -- Dasar Pemrograman Renni Angreni, S.Kom.

More Related