Anda di halaman 1dari 21

ALGORITMA

DAN STRUKTUR DATA 1


WEEK 8

Operasi File

Oleh : M. Nishom, M.Kom


Email : m.nishom.dosen@gmail.com
Telp : 081316468844
MATERI

8. Operasi File
8.1. I/O pada C++
8.2. File Header pada C++
8.3. I/O C++ dengan File
8.3.1. Membuka File
8.3.2. Menutup File
8.3.3. Menulis Operasi pada File
8.4. Praktik
z I/O pada C/C++

z I/O pada C++ (cont.)


Input Source
(keyboard, file,
Input Stream network, program)

PROGRAM
C++
Output Sink
(console, file,
Output Stream network, program)
z I/O pada C++ (cont.)

Header
z , template, dan kelas I/O pada C++

C ++ IO menyediakan file header


<iostream> (yang termasuk <ios>,
<istream>, <ostream> dan
<streambuf>), <fstream> (untuk file
IO), dan <sstream> (untuk string
IO). Selanjutnya, header <iomanip>
menyediakan manipulator seperti
setw (), setprecision () setfill () dan
setbase () untuk pemformatan.
C++ Header <iostream>
z


z I/O C++ dengan File




z I/O C++ dengan File (cont.)

Kode ini membuat sebuah file


bernama contoh.txt dan
memasukkan sebuah kalimat ke
dalamnya dengan cara yang
sama seperti yang biasa kita
lakukan dengan cout, namun
menggunakan file stream
myfile.
z Membuka File

z Mode untuk Operasi File


ios::in Open for input operations.
ios::out Open for output operations.
ios::binary Open in binary mode.
Set the initial position at the end of the file.
ios::ate If this flag is not set, the initial position is the beginning of
the file.

All output operations are performed at the end of the file,


ios::app
appending the content to the current content of the file.

If the file is opened for output operations and it already


ios::trunc existed, its previous content is deleted and replaced by the
new one.
z Membuka File

class default mode parameter


ofstream ios::out
ifstream ios::in
fstream ios::in | ios::out
z Cek Status File

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

myfile.close()
z Operasi pada File Teks

Operasi
z pada File Teks (cont.)

PRAKTIK PEMROGRAMAN
Nama File: BasicFile.cpp
z
Nama File: WritingIntoTextFile.cpp
z
Nama File: ReadingFileTeks.cpp

z
Next Week: Larik

Anda mungkin juga menyukai