465c3 contoh program operasi file

//--------------------------------------------------------------------------//PROGRAM ISI DATA MAHASISWA
//--------------------------------------------------------------------------#include
#include
#include
#include
#pragma hdrstop
//--------------------------------------------------------------------------#pragma argsused
//deklarasi record
struct mhs_rec {
char nim[11],
nama[25],
ttl[30],
jkel[2],
alamat[30];
} mhs;
FILE *fmhs;
void form_mhs()
{
gotoxy(35, 3); printf("ISI DATA MAHASISWA");
gotoxy(20, 5); printf("NIM
: ");

gotoxy(20, 7); printf("Nama
: ");
gotoxy(20, 9); printf("Tempat, Tgl Lahir : ");
gotoxy(20,11); printf("Jenis Kelamin : ");
gotoxy(20,13); printf("Alamat
: ");
}
void isi_item_mhs()
{
gotoxy(40, 5); gets(mhs.nim);
gotoxy(40, 7); gets(mhs.nama);
gotoxy(40, 9); gets(mhs.ttl);
gotoxy(40,11); gets(mhs.jkel);
if ((strcmp(mhs.jkel,"L") == 0)|| (strcmp(mhs.jkel,"l") == 0))
{gotoxy(40,11); printf("Laki-laki");}
else
{gotoxy(40,11); printf("Perempuan");}
}

gotoxy(40,13); gets(mhs.alamat);


int main(int argc, char* argv[])

{
//1. open file
fmhs=fopen("d:mahasiswa.dat", "a+");
if (fmhs == NULL)
{
printf("Open file gagal..!!");
getch();
return(0);
}
//2. tampil formulir
form_mhs();
//3. isi item data
isi_item_mhs();
//4. simpan data
fwrite(&mhs, sizeof(mhs), 1, fmhs);
//5. tutup file
fclose(fmhs);

return 0;
}
//--------------------------------------------------------------------------//--------------------------------------------------------------------------//PROGRAM REPORT DATA MAHASISWA
//--------------------------------------------------------------------------#include
#include
#include
#include
#pragma hdrstop
//--------------------------------------------------------------------------#pragma argsused
//deklarasi record
struct mhs_rec {
char nim[11],
nama[25],
ttl[30],
jkel[2],
alamat[30];
} mhs;
FILE *fmhs;
void form_reportmhs()
{

textattr(0x0b); //0x -> hexa; 0 warna background d->foreground/text
gotoxy(2, 1);

cprintf("
REPORT DATA MAHASISWA");
gotoxy(2, 3);
cprintf("--------------------------------------------------------------------");
gotoxy(2, 4);
cprintf(" NO NIM
NAMA
ALAMAT
");
gotoxy(2, 5);
cprintf("--------------------------------------------------------------------");
}
int main(int argc, char* argv[])
{
int no=0;
//1. open file
fmhs=fopen("d:mahasiswa.dat", "r");

if (fmhs == NULL)
{
printf("Open file gagal..!!");
getch();
return(0);
}
//2. tampil formulir
form_reportmhs();
//3. posisikan pointer record di awal file
fseek(fmhs, SEEK_SET, 0);
fread(&mhs, sizeof(mhs), 1, fmhs);
do {
//4. baca data/record pertama
//5 tampilkan data
no++;
gotoxy(2,5+no); cprintf(" %i %10s %-15s %-25s", no, mhs.nim, mhs.nama, mhs.alamat);
fread(&mhs, sizeof(mhs), 1, fmhs);
} while (!feof(fmhs)); //selama belum end of file
//5. tutup file
gotoxy(2, 6+no);

cprintf("--------------------------------------------------------------------");
fclose(fmhs);
getch();
return 0;
}
//--------------------------------------------------------------------------//--------------------------------------------------------------------------//PROGRAM HAPUS DATA MAHASISWA
//--------------------------------------------------------------------------#include
#include
#include
#include

#pragma hdrstop
//--------------------------------------------------------------------------#pragma argsused
//deklarasi record
struct mhs_rec {
char nim[11],
nama[25],
ttl[30],
jkel[2],
alamat[30];

} mhs;
FILE *fmhs, *ftemp;
int norec, no;
char cnim[11], jwb;
void form_mhs()
{
gotoxy(35, 3); printf("KOREKSI DATA MAHASISWA");
gotoxy(20, 5); printf("NIM
: ");
gotoxy(20, 7); printf("Nama
: ");
gotoxy(20, 9); printf("Tempat, Tgl Lahir : ");
gotoxy(20,11); printf("Jenis Kelamin : ");
gotoxy(20,13); printf("Alamat
: ");
}
void isi_item_mhs()
{
gotoxy(40, 7); gets(mhs.nama);
gotoxy(40, 9); gets(mhs.ttl);

gotoxy(40,11); gets(mhs.jkel);
if ((strcmp(mhs.jkel,"L") == 0)|| (strcmp(mhs.jkel,"l") == 0))
{gotoxy(40,11); printf("Laki-laki");}
else
{gotoxy(40,11); printf("Perempuan");}
gotoxy(40,13); gets(mhs.alamat);
}
void tampil_item_mhs()
{
gotoxy(40, 7); printf("%s",mhs.nama);
gotoxy(40, 9); printf("%s",mhs.ttl);
if ((strcmp(mhs.jkel,"L") == 0)|| (strcmp(mhs.jkel,"l") == 0))
{gotoxy(40,11); printf("Laki-laki");}
else
{gotoxy(40,11); printf("Perempuan");}

}

gotoxy(40,13); printf("%s",mhs.alamat);


void form_reportmhs()
{
textattr(0x0b); //0x -> hexa; 0 warna background d->foreground/text
gotoxy(2, 1);
cprintf("
REPORT DATA MAHASISWA");
gotoxy(2, 3);
cprintf("--------------------------------------------------------------------");
gotoxy(2, 4);
cprintf(" NO NIM
NAMA
ALAMAT
");
gotoxy(2, 5);
cprintf("--------------------------------------------------------------------");
}
int report_mhs()
{
//1. open file
fmhs=fopen("d:mahasiswa.dat", "r");

if (fmhs == NULL)
{
printf("Open file gagal..!!");
getch();
return(0);
}
//2. tampil formulir
form_reportmhs();
//3. posisikan pointer record di awal file
fseek(fmhs, SEEK_SET, 0);
fread(&mhs, sizeof(mhs), 1, fmhs);
no=0;
do {
//4. baca data/record pertama
//5 tampilkan data
no++;
gotoxy(2,5+no); cprintf(" %i %10s %-15s %-25s", no, mhs.nim, mhs.nama, mhs.alamat);
fread(&mhs, sizeof(mhs), 1, fmhs);
} while (!feof(fmhs)); //selama belum end of file
//5. tutup file

gotoxy(2, 6+no);
cprintf("--------------------------------------------------------------------");
fclose(fmhs);
getch();
} //end of report
int main(int argc, char* argv[])
{
//1. open file dengan mode R
fmhs=fopen("d:mahasiswa.dat", "r");

if (fmhs == NULL)
{
printf("Open file gagal..!!");
getch();
return(0);
}
//tampil formulir
form_mhs();
//2. isi key field/NIM
gotoxy(40,5); gets(cnim);
//3. cari nim di tabel
norec = 0;
do {
fread(&mhs, sizeof(mhs), 1, fmhs);
norec++;
} while ((strcmp(mhs.nim, cnim)!=0) && (!feof(fmhs)));
norec--;
//4. Jika ketemu
if (strcmp(mhs.nim, cnim)==0)
{
tampil_item_mhs(); //tampilkan
gotoxy(30, 17); printf("Data ini dihapus [y/t] --> ");
jwb = getchar();
if ((jwb == 'y') || (jwb == 'Y'))
{ //dihapus
ftemp=fopen("d:mahasiswa.tmp", "w");
if (ftemp == NULL)
{
printf("Open file temporary gagal..!!");
getch();
return(0);
}
fseek(fmhs, 0, SEEK_SET);
fread(&mhs, sizeof(mhs), 1, fmhs); //baca data mhs
do {
if (strcmp(mhs.nim, cnim) != 0) //jika bukan yg dihapus
fwrite(&mhs, sizeof(mhs), 1, ftemp); //simpan ke temp
fread(&mhs, sizeof(mhs), 1, fmhs); //baca data mhs
} while (!feof(fmhs));
fclose(fmhs); fclose(ftemp);
unlink("mahasiswa.dat"); //hapus file mahasiswa.dat
rename("mahasiswa.tmp", "mahasiswa.dat"); //ganti nama mhs.tmp -> mhs.dat
}
} else {
printf("Data tidak ditemukan");
getch();
}
//5. tutup file
fclose(fmhs);
clrscr();
return 0;

}
//---------------------------------------------------------------------------