c++ untuk membuka error file


Modul 4 latihan 1,a

#include <fstream.h>

#include <iomanip.h>

int main()

{

ofstream alfabet;

ifstream rekord(“latihan1.txt”);

char brs[128];

char namafile[] = “latihan1.txt”;

long htgBrs = 0L;

alfabet.open(“latihan1.txt”);

for(int hrf=’A’; hrf <=’Z’; hrf++)

alfabet << hrf << endl;

alfabet.close();

if(rekord.fail())

cerr << “Error membuka file: ” << namafile << endl;

else

{

while(!rekord.eof())

{

rekord.getline(brs, sizeof(brs));

cout << brs << endl;

if((++htgBrs % 14) == 0)

{

cout << “PRESS <ENTER>…” << endl;

cin.get();

}

}

}

rekord.close();

return 0;

}

MODUL 4 LATIHAN 2.b

#include <fstream.h>

#include <iomanip.h>

#include <string.h>

#include <iostream.h>

class Mahasiswa

{

private:

char nama[64];

char nim[9];

int sem;

float ip;

public:

Mahasiswa(){}

Mahasiswa(char [], char [], int, float);

void copy_Mahasiswa (char [], char [], int, float);

~Mahasiswa(){}

void printRekord(ofstream);

};

Mahasiswa::Mahasiswa(char nama[64], char nim[9], int sem, float ip)

{

strcpy(Mahasiswa::nama, nama);

strcpy(Mahasiswa::nim, nim);

Mahasiswa::sem = sem;

Mahasiswa::ip = ip;

}

void Mahasiswa::copy_Mahasiswa (char nama[64], char nim[9], int sem, float ip)

{

strcpy(Mahasiswa::nama, nama);

strcpy(Mahasiswa::nim, nim);

Mahasiswa::sem = sem;

Mahasiswa::ip = ip;

}

void Mahasiswa::printRekord(ofstream rekord)

{

rekord << setiosflags(ios::left);

rekord << setprecision(3);

rekord << setiosflags(ios::showpoint);

rekord << setw(16) << nama;

rekord << setw(12) << nim;

rekord << setw(5) << sem;

rekord << setw(7) << ip << endl;

}

int main()

{

ofstream rekord(“latihan2.txt”);

Mahasiswa mhs07;

char nama[64], nim[10];

int sem;

float ip;

cout << “Berikan nama: “;

cin >> nama;

cout << “Berikan nim: “;

cin >> nim;

cout << “Berikan sem: “;

cin >> sem;

cout << “Berikan ip: “;

cin >> ip;

mhs07.copy_Mahasiswa(nama, nim, sem, ip);

rekord << setiosflags(ios::left)

<< setw(16) << “Nama”

<< setw(12) << “NIM”

<< setw(5) << “Sem”

<< setw(7) << “I.P” << endl;

mhs07.printRekord(rekord);

rekord.close();

return 0;

}

Tinggalkan Balasan

Please log in using one of these methods to post your comment:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s