File I/O in C++ Dr. Taufik Fuadi Abidin, M.Tech Irvanizam 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 sebuah 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 buah distinct class untuk pemrosesan file. Satu digunakan untuk menulis sebuah file sedangkan satu lagi untuk membaca sebuah file.
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. cin adalah sebuah object dari class istream dan cout adalah sebuah object dari class ostream.
Contoh // basic file operations #include <iostream> #include <fstream> using namespace std; int main () { } ofstream myfile; myfile.open ("example.txt"); [file example.txt] Writing this to a file. myfile << "Writing this to a file.\n"; myfile.close(); return 0;
Open sebuah File Dalam C++, fungsi untuk membuka file adalahopen(). Untuk membuka sebuah file dengan sebuah objek stream object kita menggunakan fungsi open berikut: open (filename, mode); Dimanafilename adalah sebuah null-terminated character yang bertipeconst char * dan merupakan nama file yang akan dibuka. mode adalah sebuah parameter tambahan dengan kombinasi flag-flag dibawah ini:
Initializing a File ios::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 operation is typically used when you want to save a file ios::binary Open dalam mode binary. ios::ate ios::app ios::trunc 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 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. If FileName already exists, its content is destroyed and the file becomes as new
Contoh ofstream myfile; myfile.open ("example.bin", ios::out ios::app ios::binary); Setiap fungsi open() dari class ofstream, ifstream and fstream mempunyai default mode yang digunakan jika file dibuka tanpa ada arguments kedua: Class ofstream ifstream fstream Default mode parameter ios::out ios::in ios::in ios::out ofstream myfile ("example.bin", ios::out ios::app ios::binary);
Contoh Untuk membuat sebuah File #include <fstream> #include <iostream> using namespace std; int main() { char FirstName[30], LastName[30]; int Age; char FileName[20]; cout << "Enter First Name: "; cin >> FirstName; cout << "Enter Last Name: "; cin >> LastName; cout << "Enter Age: "; cin >> Age; cout << "\nenter the name of the file you want to create: "; cin >> FileName; ofstream Students(FileName, ios::out); Students << FirstName << "\n" << LastName << "\n" << Age; cout << "\n\n"; return 0; }
Contoh untuk Membuka Sebuah File #include <fstream> #include <iostream> using namespace std; int main() { char FirstName[30], LastName[30]; int Age; char FileName[20]; cout << "Enter the name of the file you want to open: "; cin >> FileName; ifstream Students(FileName); Students >> FirstName >> LastName >> Age; cout << "\nfirst Name: " << FirstName; cout << "\nlast Name: " << LastName; cout << "\nenter Age: " << Age; cout << "\n\n"; return 0; }
Fungsi is_open() Kadang-kadang file yang mau dibuka tidak berhasil dibuka, maka perlu dicek dulu sebelum dibuka filenya. Fungsi untuk pengecekan buka file adalahis_open() Fungsi ini mereturn nilai boolean (TRUE or FALSE) 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 fungsiclose()untuk menutup file yang telah dibuka. ofstream myfile ("example.txt"); myfile.close(); Kalau sudah memanggil fungsi close(), jika untuk membuka file lagi, maka harus dipanggil fungsi open() kembali.
Text files Untuk stream text files, kita tidak memasukkan flag ios::binary dalam mode pembukanya. File-file tersebut didesain untuk memasukkan text dan lalu kita input atau output karakter-karakter dari/ke file tersebut.
Contoh Text Files 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 // writing on a text file #include <iostream> #include <fstream> using namespace std; int main () { ofstream myfile ("example.txt"); if (myfile.is_open()) { myfile << "This is a line.\n"; myfile << "This is another line.\n"; myfile.close(); } else cout << "Unable to open file"; return 0; } [file example.txt] This is a line. This is another line.
Input data menggunakan cin 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 // reading a text file #include <iostream> #include <fstream> #include <string> using namespace std; int main () { string line; ifstream myfile ("example.txt"); } if (myfile.is_open()) { while ( myfile.good() ) { getline (myfile,line); cout << line << endl; } myfile.close(); } else cout << "Unable to open file"; return 0; This is a line. This is another line.
References http://www.functionx.com/cpp/articles/filestreaming.htm http://www.cplusplus.com/doc/tutorial/files/ http://www.cstutoringcenter.com/tutorials/cpp/cpp9.php