You are on page 1of 2

Algoritma Pemrograman

MODUL 12 – FILE

Silahkan coba dan pelajari program yang ada dalam modul 12 ini. Apabila anda mengalami
kesulitan atau ada pertanyaan silahkan ditanyakan atau didiskusikan di forum diskusi di Elena.
1. Contoh-contoh Program
Silahkan coba program berikut.

#include <iostream>
#include<conio.h>
#include<fstream>
#include<iomanip>
using namespace std;

int main(){
ofstream file_out;
file_out.open("me.TXT");
file_out <<"I am a student"<<endl
<<"who is never tired of seeking knowledge "<<endl
<<endl;
file_out.close();
getch();
}
Coding 12.1. Program FILE 1

Lanjutkan dengan program berikut.

#include <iostream>
#include<conio.h>
#include<fstream>
#include<iomanip>
using namespace std;

int main(){
ofstream file_out;
file_out.open("me.TXT",ios::app);
file_out
<<"I am a student"<<endl
<<"who always wants to make the best results, I ooh ..."<<endl<<endl;
file_out.close();
getch();
}
Coding 12.2. Program FILE 2
Algoritma Pemrograman
Lanjutkan dengan program berikut.

#include <iostream>
#include <conio.h>
#include <fstream>
using namespace std;

int main() {
system("CLS");
const int MAKS = 80;
char supporter [MAKS+1];
ifstream file_input ("me.TXT");
while (file_input)//read all of data {
file_input.getline (supporter,MAKS);
cout<<supporter<<endl;
}
file_input.close();
getch(); }
Coding 12.3. Program FILE 3

Lanjutkan dengan program berikut.

#include <iostream>
#include <conio.h>
#include <fstream>
using namespace std;
int main() {
//stream for writing file
ofstream myfile;
//opening file,
//if file isnot found then file will automatically be made
myfile.open("TEST.txt", ios::app);

cout<<"OPERASI FILE 1"<<endl;


cout<<"--------------"<<endl;
//fail() -> to check a error in file operation
if(!myfile.fail()) {
//write into file
myfile<<"LEARNING FILE OPERATION"<<endl;
myfile.close(); //close file
cout<<"Text have been written into File"<<endl;
}
else {
cout<<"File is not found"<<endl;
}
getch();
return 0; }

Coding 12.4. Program FILE 4


2. Tugas Terstruktur
1. SIAPKAN TUGAS AKHIR MATA KULIAH

You might also like