Lab Latihan #1
Pelajaran 6 dari 23 dalam kursus Penanganan File C++ di Coddy.
Tantangan
Mudah*Jangan ubah kode yang tidak seharusnya kamu ubah*
Coba buka 3 file:
code.txtscript.txtnovel.txt
Buka file-file tersebut dan periksa satu per satu sesuai urutan, apakah masing-masing bisa dibuka. Jika bisa, cetak pesan File opened, jika tidak, cetak Unable to open file.
Setelah itu, tutup hanya file code.txt dan novel.txt.
*Cetak setiap pesan di baris baru*
Coba sendiri
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
//-------------------------
ifstream file_code;
ifstream file_script;
ifstream file_novel;
// Masukkan kodemu di sini
//-------------------------
// Jangan ubah kode di bawah ini
if(!file_code.is_open())
cout << "File closed" << endl;
else
cout << "File still opened" << endl;
if(!file_script.is_open())
cout << "File closed" << endl;
else
cout << "File still opened" << endl;
if(!file_script.is_open())
cout << "File closed" << endl;
else
cout << "File still opened" << endl;
return 0;
}Semua pelajaran di Penanganan File C++
Latihan sendiri: Compiler C++ online