Sabtu, 09 Januari 2010

Program database sederhana menggunakan C

hi, hmm beberapa kali saya menulis artikel tentang java nah kali ini saya akan menulis tentang c
mungkin masih sederhana sekali di banding senior2 heheheh
nih source codenya



#include <stdio.h>
#include <stdlib.h>

#define FALSE 0
#define TRUE 1

void main (void);
void add_to_file(char *filename);
void show_contents(char *filename);
void get_record(char *ptr_date,float *ptr_main_temp,float *ptr_max_temp, float *ptr_windspeed, float *ptr_rainfall);

void main(void)
{
     char filename[01];
     int choice;
    
     while (choice !=3)
     {
           /* display list of choices */
           printf("\n \n \t \t MAIN MENU");
           printf("\n \t 1 - Create new file or add to existing file");
           printf("\n \t 2 - List contents of an existing file");
           printf("\n \t 3 - Exit program");
                      choice = 0;
                      /* get input */
                      while (choice < 1 || choice >3)
                      {
                            printf("\n \t Your choice (1,2 or 3) ?");
                            scanf("%d", &choice);
                            }
                            if (choice != 3 )
                            {
                                       printf ("\n \t Enter filename : ");
                                       scanf("%s", filename);
                                              switch (choice)
                                       {
                                              case 1:
                                              add_to_file(filename);
                                              break;
                                              case 2:
                                            show_contents(filename);
                                              break;
                                              }    
                                       }
                                      
           }
           }
          
           void add_to_file(char *filename)
           {
                FILE *file_ptr;
                char date[21],buffer[01];
               
                float min_temp,max_temp,windspeed,rainfall;
                int exit_flag = FALSE;
               
                /*open file*/
                file_ptr = fopen(filename, "w+");
                if (file_ptr == NULL)
                {
                             printf ("\n Unable to open file %s", filename);
                             return;
                             }
                             //read and write records
                             while (!exit_flag)
                             {
                                   get_record(date,&min_temp,&max_temp,&windspeed,&rainfall);
                                   fprintf(file_ptr, "%s %f %f %f %f \n", date,min_temp,max_temp,windspeed,rainfall);
                                   printf("\n Do uou want to enter more data - Y/N ? ");
                                   scanf ("%s", buffer);
                                   if (buffer [0] == 'N' || buffer [1] == 'n')
                                   exit_flag = TRUE;
                                   }
                                  
                                   fclose(file_ptr);
}

void get_record(char *ptr_date, float *ptr_min_temp, float *ptr_max_temp, float *ptr_windspeed, float *ptr_rainfall)
{
     printf ("\nEnter date of record : ");
     scanf ("%s", ptr_date);
     printf ("Enter minimum temperatur : ");
     scanf ("%f", ptr_min_temp);
     printf ("Enter maximum temperatur : ");
     scanf ("%f", ptr_max_temp);
     printf ("Enter maximum windspeed : ");
     scanf ("%f", ptr_windspeed);
     printf ("Enter total rainfall : ");
     scanf("%f", ptr_rainfall);
    
 }

 void show_contents(char *filename)
 {
      FILE *file_ptr;
      char date[21];
      float min_temp,max_temp,windspeed,rainfall;
      file_ptr = fopen(filename, "r");
      if (file_ptr == NULL)
      {
                   printf ("\n Unable to open file %s", filename);
                   return;
                   }
                   printf ("\n Date      Min Temp       Max Temp          ");
                   printf ("Windspeed    Rainfall"); 
                   /* read records from file and print */
                   while (!feof(file_ptr))
                   {
                         fscanf(file_ptr, "%s %f %f %f %f \n", date,&min_temp,&max_temp,&windspeed,&rainfall);
                         printf ("\n %s   %f      %f          %f     %f ", date,min_temp,max_temp,windspeed,rainfall);
                          }
                         /*close file*/
                         fclose(file_ptr);
  }

The fread() function

The fread() function reads a block of bytes from a file. The function requires four arguments , as follows

fread(ptr, size, num, infile_ptr)

The fwrite() function
The fwrite() function writes a block of bytes to a file. The arguments to fwrite() are identical to those for fread()

fwrite(ptr, size, num, outfile_ptr)



output :

Tidak ada komentar:

Posting Komentar