15
File I/O in C++ Dr. Taufik Fuadi Abidin, M.Tech Irvanizam Zamanhuri, M.Sc

INF103-01

Embed Size (px)

DESCRIPTION

INF103-01

Citation preview

Page 1: INF103-01

File I/O in C++

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

Page 2: INF103-01

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.

Page 3: INF103-01

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.

Page 4: INF103-01

Contoh

// basic file operations

#include <iostream>

#include <fstream>

using namespace std;

int main () {

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

ofstream myfile;

myfile.open ("example.txt");

myfile << "Writing this to a file.\n";

myfile.close();

return 0;

}

Page 5: INF103-01

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:

Page 6: INF103-01

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

Page 7: INF103-01

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

Page 8: INF103-01

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;

}

Page 9: INF103-01

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;

}

Page 10: INF103-01

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 */ }

Page 11: INF103-01

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();

Page 12: INF103-01

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.

Page 13: INF103-01

Contoh Text Files1234567

// writing on a text file#include <iostream>

#include <fstream>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 << "This is a line.\n"; myfile << "This is another line.\n"; myfile.close();

} elsecout << "Unable to open file";

return 0; }

Page 14: INF103-01

Input data menggunakan cin123456789

1011

// reading a text file#include <iostream>#include <fstream>#include <string>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 << line << endl;

} myfile.close();

} elsecout << "Unable to open file";

return 0; }

Page 15: INF103-01

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