Keterangan :
Mode adalah untuk menentukan fungsi dari file data
yang diaktifkan, yang antara lain terdiri dari :
- w : Sebagai tempat menampung data yang
diinputkan. Data yang pertama diinputkan
setiap memulai menjalankan program akan
selalu menempati posisi record yang pertama
(dimulai dari nomor record 0)
- r : Sebagai tempat untuk membaca data dari file
data
-a : Sama seperti w, tetapi dengan mode ini setiap
data yang diinputkan akan selalu menempati
posisi sesudah data record yang terakhir yang
terdapat dalam file data.
3. Menyimpan data ke dalam file data
Bentuk Umum :
fwrite (&variabel struktur,sizeof(variabel struktur),1,pointer file);
fwrite (&variabel record,sizeof(variabel record),1,variabel file);
Catt: variabel struktur = variabel record (vr)
fwrite (&vr,sizeof(vr),1,vf);
4. Membaca data dari dalam file data
Bentuk Umum :
fread (&variabel struktur,sizeof(variabel struktur),1,pointer file);
fread (&variabel record,sizeof(variabel record),1,variabel file);
Atau
fread (&vr,sizeof(vr),1,vf);
5. Menutup file data
Bentuk Umum :
fclose(pointer file); atau fclose(vf);
40
#include<stdio.h>
#include<conio.h>
main()
{
/*Deklarasi Pointer File*/
FILE *vf;
char td=Y,sd=y;
/*Deklarasi data buku bertipe struktur*/
struct
{
char jb[25];
char pb[25];
int jml;
}vr;
/*Buka File*/
vf=fopen(dbuku.dat,w);
if (vf==NULL)
{
clrscr();
printf(File data tidak dapat dibuat !);
getch(); exit(1);
}
while(td==y || td==Y)
{ clrscr();
/*Input data buku*/
gotoxy(25,5) ; printf(Input Data Buku );
gotoxy(25,6) ; printf(----------------------);
gotoxy(25,8) ; printf(Input judul buku :);
gotoxy(25,9) ; printf(Input pengarang buku :);
gotoxy(25,10) ; printf(Input jumlah buku :);
gotoxy(25,11) ; printf(Simpan Data [Y/T] ? :);
gotoxy(25,12) ; printf(Tambah Data [Y/T] ? :);
gotoxy(40,8); gets(vr.jb);
gotoxy(40,9); gets(vr.pb);
gotoxy(40,10); scanf(%i,&vr.jml); fflush(stdin);
gotoxy (40,11);sd= =getch();fflush(stdin);
/*Simpan data buku*/
if (sd==Y || sd==y)
{ fwrite(&vr,sizeof(vr),1,vf); }
else
{gotoxy (40,14);printf(Data Tidak Disimpan); }
gotoxy(40,12);scanf(%c,&td); fflush(stdin);
}
15 25 40
5 Entry Data Fotocopy MENU PROGRAM
6 ~~~~~~~~~~~~~~~
7 Tanggal Transaksi : x[10] [1] ENTRY
8 Nama Pelanggan : x[15] [2] LAPORAN
9 Jenis Mesin [A/B/C] : x [3] EXIT
10 Jumlah Lembar : 999 INPUT PILIHAN [1-3] :
11 Data Sudah Benar ? [Y/T] : x
12 Masih Ada Data ? [Y/T] : x
Informasi 20
1 Laporan Data Transaksi Fotocopy Khairunnisa
2 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3
4 No. Tanggal Nama Pelanggan Jenis Tarif/ Jumlah Total
5 Transaksi Mesin Lembar Lembar Tarif
6
7
1 6 17 33 39 46 53 64
Tarif perlembar ditentukan berdasarkan jenis mesin
fotocopy dengan ketentuan sebagai berikut :
Jika jenis mesin=A, maka tarif perlembar Rp. 75,-
Jika jenis mesin=B, maka tarif perlembar Rp. 100,-
Jika jenis mesin=C, maka tarif perlembar Rp. 125,-
Total Tarif diperoleh dari Jumlah Lembar fotocopy dikalikan
dengan Tarif perlembar.
PROCEDURE
1 Entry Data Pembelian Barang
No. Faktur : x[5]
MENU PROGRAM
Nama Barang
Harga Beli
: x[15]
: 99999
Jumlah Pembelian : 999
3 [1] ENTRY
[2] LAPORAN
Tanggal Beli : xx-xx-xxxx [3] EXIT
Simpan Data [Y/T] : x INPUT PILIHAN [1-3] :
Tambah Data[Y/T] : x
2 NO Nama
Laporan Pembelian Barang
Harga Jumlah Tanggal Total Harga Nilai
Barang Beli Pembelian Harga Jual Persediaan
1
entry()
{
FILE * vf;
char sd,td='y';
struct
{char nf[5] ;
char nb[15];
long int hg;
long int jb;
char tgl[10];
}vr;
vf=fopen("brg.dat","a");
if (vf==NULL)
{ clrscr();
gotoxy(25,23);printf("Data Tidak dibuat");
getch();
exit();
}
while (td=='y' || td=='Y')
{
clrscr();
gotoxy(25,1) ;printf("ENTRY DATA BARANG");
gotoxy(25,2) ;printf("-----------------");
gotoxy(25,4) ;printf("NOMOR FAKTUR :");
gotoxy(25,5) ;printf("NAMA BARANG :");
gotoxy(25,6) ;printf("HARGA BELI :");
gotoxy(25,7) ;printf("JUMLAH PEMBELIAN :");
gotoxy(25,8) ;printf("TANGGAL BELI :");
gotoxy(25,9) ;printf("SIMPAN DATA[Y/T] :");
gotoxy(25,10);printf("TAMBAH DATA[Y/T] :");
gotoxy(50,4) ;gets(vr.nf);
gotoxy(50,5) ;gets(vr.nb);
gotoxy(50,6) ;scanf("%li",&vr.hg);
gotoxy(50,7) ;scanf("%li",&vr.jb);fflush(stdin);
gotoxy(50,8) ;gets(vr.tgl);fflush(stdin);
gotoxy(50,9) ;scanf("%c",&sd);fflush(stdin);
if (sd=='y' || sd=='Y')
{
fwrite(&vr,sizeof(vr),1,vf);
}
else
{
gotoxy(25,13);printf("DATA TIDAK DISIMPAN");
}
gotoxy(50,10);scanf("%c",&td);fflush(stdin);
}
fclose(vf);
}
laporan()
{
FILE * vf;
float hj,np,th;
int br,no;
struct
{char nf[5] ;
char nb[15];
long int hg;
long int jb;
char tgl[10];
}vr;
vf= fopen("brg.dat","r");
if (vf==NULL)
{ clrscr();
gotoxy(25,23);printf("Data Tidak dibuat");
getch();
exit();
}
clrscr();
gotoxy(15,1);printf("LAPORAN DATA BARANG");
gotoxy(1,2);printf("---------------------------------------------------------------------------");
gotoxy(1,3);printf("| NO| NAMA BARANG | HARGA | JUMLAH |TANGGAL |TOTAL | HARGA |
NILAI |");
gotoxy(1,4);printf("| | | BELI | PEMBELIAN| |HARGA | JUAL | PERSEDIAAN|");
gotoxy(1,5);printf("---------------------------------------------------------------------------");
/*
12345678901234567890123456789012345678901234567890123456789012345678901234567
8901234567890
1 2 3 4 5 6 7 8*/
br=6;no=0;
while (fread(&vr,sizeof(vr),1,vf))
{
no=no+1;
hj=0.1*vr.hg+vr.hg;
np=vr.jb*hj;
th=vr.hg*vr.jb;
gotoxy(1,br);printf("%s" "%i","|",no);
gotoxy(5,br);printf("%s" "%s","|",vr.nb);
gotoxy(20,br);printf("%s" "%li","|",vr.hg);
gotoxy(28,br);printf("%s" "%li","|",vr.jb);
gotoxy(39,br);printf("%s" "%s","|",vr.tgl);
gotoxy(48,br);printf("%s" "%f","|",th);
gotoxy(55,br);printf("%s" "%f","|",hj);
gotoxy(63,br);printf("%s" "%f","|",np);
gotoxy(75,br);printf("%s" ,"|");
br=br+1;
}
gotoxy(1,br);printf("---------------------------------------------------------------------------");
fclose(vf);
getche();
}
main()
{
int pil=0;
while(pil < 3)
{
clrscr();
gotoxy (25, 5) ; printf("MENU PILIHAN");
gotoxy (25, 6) ; printf([1] ENTRY ");
gotoxy (25, 7) ; printf([2] LAPORAN");
gotoxy (25, 8) ; printf([3] EXIT");
gotoxy (25, 9) ; printf(Input Pilihan [1-3]:");scanf("%i",&pil);fflush(stdin);
if (pil==1)
{ entry(); continue; }
else
if (pil==2)
{ laporan(); continue; }
}
}
FIELD KUNCI
Entry Data Pembelian Barang MENU PROGRAM
No. Faktur : 999
Nama Barang : x[15]
[1] ENTRY
Harga Beli : 99999
[2] LAPORAN
Jumlah Pembelian : 999
Tanggal Beli : xx-xx-xxxx [3] EXIT
Simpan Data [Y/T] : x INPUT PILIHAN [1-3] :
Tambah Data[Y/T] : x