0

Contoh Operasi FIle

Posted by Jujur Sitanggang on 4:12 PM
Contoh Operasi File Tambah,Edit,Hapus,cari dengan C


#include <stdio.h>

#include <stdlib.h>



//deklarai fungsi

  void buka();

  void baca();

  void cari2();

  void tambah();

  void tampil();

  void cari();

  void edit();

  void hapus();





  //deklarasi struct

  typedef struct

  {

         int no;

         long int kodemusik;

         char jenis_musik[30];

         char artis[30];

         char album[30];

         char judul_lagu[30];

  }Musik;



  //deklarasi variabel struct

  Musik music;



  //deklarasi variabel



  FILE *pf;

  int menu;

  char back2menu, jawab;

  int i, nomor, record, record_ubah, hasil_baca, banding;

  int z,hasil_baca;

  int tdkketemu;

  long int ofset_byte, kodemusik_ubah;



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

{

  do

  {

   system("cls");

   printf("              Data Musik     \n");

   printf(" ----------------------------------------\n\n");

   printf("   1.Tambah Data                                \n");

   printf("   2.Tampil Data                                \n");

   printf("   3.Cari Data                                \n");

   printf("   4.Edit                                \n");

   printf("   5.Hapus                                   \n");

   printf("   6.Keluar                                \n\n");

   printf(" ----------------------------------------\n\n");

   printf(" Pilih = ");scanf("%i",&menu);

   switch(menu)

   {

               case 1 : tambah();break;

               case 2 : tampil();break;

               case 3 : cari(); break;

               case 4 : edit(); break;

               case 5 : hapus();break;

               case 6 : exit(1);break;

   }

   printf("\n\n");

   printf("Tekan Y untuk kembali ke Main Menu : ");

   back2menu=getche();

   }

   while((back2menu=='Y')||(back2menu=='y'));

  system("PAUSE");

  return 0;

}



void buka()

{

     //Buka File

     if((pf=fopen("data.txt","at"))==NULL)

     {

         printf("Fatal Error : Data Tidak Dapat dibaca!\n");

         exit(1);

     }

}



void baca()

{

     //baca File

     if((pf=fopen("data.txt","rt"))==NULL)

     {

       printf("Fatal Error : Data Tidak Dapat dibaca!\n");

       exit(1);

     }

}

void tambah()

{

     buka();

     //do

     //{



         printf("            Data Musik     \n");

         printf("---------------Tambah Data-------------\n\n");

         printf("----------------------------------------\n\n");



         printf("Kode Musik \t: ");scanf("%li", &music.kodemusik);

         printf("Jenis Musik\t: ");fflush(stdin);gets(music.jenis_musik);

         printf("Artis\t: ");fflush(stdin); gets(music.artis);

         printf("Album\t: ");fflush(stdin); gets(music.album);

         printf("Judul Lagu\t: ");fflush(stdin); gets(music.judul_lagu);

         if (buka != NULL)
         {
             fprintf(buka,"Kode Musik : %s \n",music.kodemusik);
             fprintf(buka,"Jenis Musik : %s \n",music.jenis_musik);
             fprintf(buka,"Artis : %s \n",music.artis);
             fprintf(buka,"Album : %s \n",music.album);
             fprintf(buka,"Judul lagu : %s \n",music.judul_lagu)
             fprintf(buka,"---------------------------------------------\n");
         }
         else
         {
                                 printf("Fatal error : data.txt tidak bisa dibuka");
                                exit(EXIT_FAILURE);
                          }


         music.no=record;

         fwrite(&music,sizeof(music),1,pf);

         fclose(pf);

}



void tampil()

{

     system("cls");

     baca();

     nomor=1;



     printf("            Data Musik     \n");

     printf("---------------Tampil Data-------------\n\n");

     printf("----------------------------------------\n\n");

     printf("No    KodeMusik     Jenis Musik     Artis     Album      Judul Lagu   \n");



     for(;;)

     {

       fread(&music,sizeof(music),1,pf);

       if(feof(pf))break;

       printf("%-5i %-16li %-13s %-8s %-9s %-13s \n",nomor++,music.kodemusik,music.jenis_musik,music.artis,music.album,music  .judul_lagu);

     }

     printf("-----------------------------------------------/n");



     //tutup File

     fclose(pf);

}



void cari()

{

     baca();

     printf("\n\n");

     printf("       Masukan Kode Musik : ");scanf("%d",&z);



     tdkketemu=1;

     while(!feof(pf))

     {

         if(z==music.kodemusik)

         {

             tdkketemu=0;

             system("cls");

             printf("                Data Musik    \n");

             printf("              Pencarian Musik    \n");

             printf("    =========================================   \n\n");

             printf("    ---------------------------------------  \n");

             printf("    Kode Buku \t :%li\n", z);

             printf("    Jenis Musik \t :%s\n", music.jenis_musik);

             printf("    Artis \t :%s\n", music.artis);

             printf("    Album \t :%s\n", music.album);

             printf("    Judul Lagu \t :%s\n", music.judul_lagu);

             printf("    ---------------------------------------  \n");



             break;



         }

         else

         fread(&music,sizeof(music),1,pf);

     }



     if(tdkketemu==1)

     {

         printf("    Data Tidak Ditemukan !");

     }



     //Tutup File

     fclose(pf);

}



void baca_edit()

{

    if((pf=fopen("data.txt","rt+"))==NULL)

     {

       printf("Fatal Error : Data Tidak Dapat dibaca!\n");

       exit(1);

     }

}



void edit(){

     baca_edit();

     printf("\n\n");

     printf("       Masukan Kode Musik : ");scanf("%d",&z);



     tdkketemu=1;

     while(!feof(pf))

     {

        if(z==music.kodemusik)

         {



             tdkketemu=0;

             printf("Musik Ditemukan!");

             record_ubah=music.no;



             ofset_byte=(record_ubah-1)*sizeof(music);

             fseek(pf,ofset_byte, SEEK_SET);

             hasil_baca=fread(&music,sizeof(music),1,pf);



             if(hasil_baca==0)

             {

                printf(" Nomor record Tidak Sesuai !\r\n");

             }

             else

             {

             printf("            Data Musik     \n");

             printf("          Pencarian Musik   \n");

             printf("    ---------------------------------------  \n");

             printf("    Kode Buku \t :%li\n", z);

             printf("    Jenis Musik \t :%s\n", music.jenis_musik);

             printf("    Artis \t :%s\n", music.artis);

             printf("    Album \t :%s\n", music.album);

             printf("    Judul Lagu \t :%s\n", music.judul_lagu);

             printf("    ---------------------------------------  \n");



             printf("\n\n");

             printf("----------------------------------------------------\n");

             printf(" Data Yang Akan Diubah\n");

             printf("----------------------------------------------------\n");

             printf("Kode Music \t:");scanf("%li", &music.kodemusik);

             printf("Jenis Music\t:");fflush(stdin);gets(music.jenis_musik);

             printf("Artis\t:");fflush(stdin);gets(music.artis);

             printf("Album\t:");fflush(stdin);gets(music.album);

             printf("Judul Lagu\t:");fflush(stdin);gets(music.judul_lagu);

             }

             music.no=record_ubah;



             fseek(pf,ofset_byte,SEEK_SET);



             fwrite(&music,sizeof(music),1,pf);

             break;



         }

         else

         {

             fread(&music,sizeof(music),1,pf);

         }

     }



     if(tdkketemu==1)

     {

         printf("    Data Tidak Ditemukan !");

     }



     //Tutup File

     fclose(pf);

}



void hapus()

{

     FILE *pfHapus;

     long int pstr1, pstr2;

     int banding;

     long int reqkodemusik;



     do{

     system("cls");

     baca();



     printf(" --------------------------------------------------------------\n");

     printf(" ------------------------Hapus Data Musik-----------------------\n");

     printf(" --------------------------------------------------------------\n");

     printf("       Masukan Kode Musik : ");scanf("%li",&reqkodemusik);



     tdkketemu=1;

     while(!feof(pf))

     {

        if(reqkodemusik==music.kodemusik)

         {



             tdkketemu=0;

             system("cls");

             printf("Musik Ditemukan!");

             record_ubah=music.no;



             printf("            Data Musik    \n");

             printf("              Pencarian Musik     \n");

             printf("    ---------------------------------------  \n");

             printf("    Kode musik \t :%li\n", reqkodemusik);

             printf("    Jenis Musik\t :%s\n", music.jenis_musik);

             printf("    Artis \t :%s\n", music.artis);

             printf("    Album \t :%s\n", music.album);

             printf("    Judul Lagu \t :%s\n", music.judul_lagu);

             printf("    ---------------------------------------  \n");



             music.no=record_ubah;



             for(;;){

             fread(&music, sizeof(music), 1, pf);

             if(feof(pf))break;



             //strcmp: membandingkan apa yg dicari dengan kode_musik yang ada di dalam file

             //strlwr: string to lower, mengecilkan semua karakter

             pstr1 = reqkodemusik;

             pstr2 = music.kodemusik;

             if (pstr1==pstr2)

             {

              banding=0;

              }else

              {

              banding=1;

              }

              //banding = strcmp(strlwr(pstr1), strlwr(pstr2));



              if(banding != 0){

              //open file

              if((pfHapus = fopen("Temp.txt", "at")) == NULL){

              system("cls");

              printf("File tidak dapat dibaca !!\n");

              exit(1);

              }//nif



              //di fwrite ke file baru, selain yg dicari

              fwrite(&music,sizeof(music),1,pfHapus);

              fclose(pfHapus);



              }//nif



              }//nfor

              //tutup file

              fclose(pf);



              //rename file temp dengan current name

              remove("data.txt");

              rename("Temp.txt", "data.txt");



             break;





         }

         else

         {

             fread(&music,sizeof(music),1,pf);

         }

     }



     if(tdkketemu==1)

     {

         printf("    Data Tidak Ditemukan !");

     }





     printf("\n\n Mau menghapus data lagi (Y/T)?");

     jawab = getche();

}while (jawab=='Y'||jawab=='y');

}


 

Source code Operasi File Tambah,Edit,Hapus,cari dengan C

Source code Operasi File Tambah,Edit,Hapus,cari dengan C
Source code Operasi File Tambah,Edit,Hapus,cari dengan C Source code Operasi File Tambah,Edit,Hapus,cari dengan C
Source code Operasi File Tambah,Edit,Hapus,cari dengan C

0 Comments

Copyright Jujur Soaloon Sitangang Lipan All rights reserved. Theme by Sitanggang. | Bloggerized by Soalparna.