O in C++ -...

download O in C++ - informatika.unsyiah.ac.idinformatika.unsyiah.ac.id/irvanizam/teaching/prog2/INF103-01.pdf · int main { [file example.txt] ... Dalam C++, fungsi untuk membuka file adalah

If you can't read please download the document

Transcript of O in C++ -...

  • File I/O in C++

    Dr. Taufik Fuadi Abidin, M.TechIrvanizam Zamanhuri, M.Sc

  • Pemrosesan File dalam C++ Pemrosesan file dalam C++ dilakukan dengan menggunakan

    fstream class. Tidak seperti struktur FILE, fstream merupakan sebuah complete class dengan constructors, sebuah destructor and overloaded operators.

    Untuk melakukan pemrosesan File, kita bisa mendeklerasikan Untuk melakukan pemrosesan File, kita bisa mendeklerasikansebuah instance dari sebuah objek fstream. Jika nama file yang mau diproses tidak diketahui, maka cukup gunakan default constructor.

    Tidak seperti struktur FILE, fstream class menyediakan dua buahdistinct class untuk pemrosesan file. Satu digunakan untukmenulis sebuah file sedangkan satu lagi untuk membaca sebuahfile.

  • Input/Output File C++ menyediakan class-class berikut untuk melakukan

    output dan input karakter-kareakter dari/ke file-file.- ofstream: class untuk menulis karakter ke file-file.- ifstream: class untuk membaca karakter dari file-file.- fstream: class untuk menulis dan membaca karakter dari/ke file-file.

    Class-class tersebut dari classe istream, dan ostream. Class-class tersebut dari classe istream, dan ostream. cin adalah sebuah object dari class istream dan cout

    adalah sebuah object dari class ostream.

  • Contoh

    // basic file operations

    #include

    #include

    using namespace std;

    int main () {

    [file example.txt] Writing this to a file.

    ofstream myfile;

    myfile.open ("example.txt");

    myfile

  • Open sebuah File Dalam C++, fungsi untuk membuka file adalah open(). Untuk membuka sebuah file dengan sebuah objek stream

    object kita menggunakan fungsi open berikut:open (filename, mode);

    Dimana filename adalah sebuah null-terminated Dimana filename adalah sebuah null-terminated character yang bertipe const char * danmerupakan nama file yang akan dibuka.

    mode adalah sebuah parameter tambahan dengankombinasi flag-flag dibawah ini:

  • Initializing a Fileios::in If FileName is a new file, then it gets created fine as an empty file.

    If FileName already exists, then it is opened and its content is made available for processing

    ios::out If FileName is a new file, then it gets created fine as an empty file. Once/Since it gets created empty, you can write data to it.If FileName already exists, then it is opened, its content is destroyed, and the file becomes as new. Therefore you can create new data to write to it. Then, if you save the file, which is the main purpose of this mode, the new content is saved it.*This the file, which is the main purpose of this mode, the new content is saved it.*This operation is typically used when you want to save a file

    ios::binary Open dalam mode binary.

    ios::ate If FileName is a new file, data is written to it and subsequently added to the end of the file. If FileName already exists and contains data, then it is opened and data is written in the current position

    ios::app If FileName is a new file, data is written to it.If FileName already exists and contains data, then it is opened, the compiler goes to the end of the file and adds the new data to it.

    ios::trunc If FileName already exists, its content is destroyed and the file becomes as new

  • Contohofstream myfile; myfile.open ("example.bin", ios::out | ios::app | ios::binary);

    Setiap fungsi open() dariclass ofstream, ifstream and fstream mempunyai default mode yang digunakan jika file dibuka tanpa ada arguments kedua:

    ofstream myfile ("example.bin", ios::out | ios::app | ios::binary);

    Class Default mode parameter

    ofstream ios::out

    ifstream ios::in

    fstream ios::in | ios::out

  • Contoh Untuk membuat sebuah File#include

    #include

    using namespace std;

    int main() {

    char FirstName[30], LastName[30];

    int Age; char FileName[20];

    cout > FirstName;

    cout > LastName;

    cout > Age;

    cout > FileName;

    ofstream Students(FileName, ios::out);

    Students

  • Contoh untuk Membuka Sebuah File#include

    #include

    using namespace std;

    int main() {

    char FirstName[30], LastName[30];

    int Age; char FileName[20];

    cout > FileName;

    ifstream Students(FileName);

    Students >> FirstName >> LastName >> Age;

    cout

  • Fungsi is_open() Kadang-kadang file yang mau dibuka tidak berhasil dibuka,

    maka perlu dicek dulu sebelum dibuka filenya. Fungsi untuk pengecekan buka file adalah is_open() Fungsi ini mereturn nilai boolean (TRUE or FALSE) Berikut pemanggilan fungsi is_open() dalam statement IF Berikut pemanggilan fungsi is_open() dalam statement IF

    if (myfile.is_open()) { /* ok, proceed with output */ }

  • Closing a file Ketika kita mengakhiri operasi input/output pada file, kita

    harus menutup file tersebut. Dalam C++ terdapat fungsi close()untuk menutup file

    yang telah dibuka.

    ofstream myfile ("example.txt");

    Kalau sudah memanggil fungsi close(), jika untuk membukafile lagi, maka harus dipanggil fungsi open() kembali.

    ofstream myfile ("example.txt");myfile.close();

  • Text files Untuk stream text files, kita tidak memasukkan flag ios::binary dalam mode pembukanya.

    File-file tersebut didesain untuk memasukkan text dan lalukita input atau output karakter-karakter dari/ke file tersebut.

  • Contoh Text Files1234567

    // writing on a text file#include

    #include using namespace std; int main () {

    ofstream myfile ("example.txt");

    [file example.txt] This is a line. This is another line.

    789

    10111213141516

    ofstream myfile ("example.txt"); if (myfile.is_open()) {

    myfile

  • Input data menggunakan cin123456789

    1011

    // reading a text file#include #include #include using namespace std; int main () {

    string line; ifstream myfile ("example.txt");

    This is a line. This is another line.

    11121314151617181920212223

    ifstream myfile ("example.txt"); if (myfile.is_open()) {

    while ( myfile.good() ) { getline (myfile,line); cout

  • References http://www.functionx.com/cpp/articles/filestreaming.htm http://www.cplusplus.com/doc/tutorial/files/ http://www.cstutoringcenter.com/tutorials/cpp/cpp9.php