Wednesday, November 12, 2014

How To Read and Write File in C

Database FoodDB.txt must be in the same folder with this program


#include <stdio.h>
#include <conio2.h>
#include <string.h>

// Create data structure 'items'
typedef struct
{
    char ItemsCode[6],
         ItemsName[51];
         
    unsigned int Price,
                 Qty,
                 MinimumQty,
                 MaximumQty;
} items;
 
// DeleteItems function used to delete item from database
void DeleteItems(items data[])
{
    signed int index;
    
    // We assigned the index from each variable to zero
    for(index = 0; index < 1000; index++)
    {
        data[index].Price = 0;
        data[index].Qty = 0;
        data[index].MinimumQty = 0;
        data[index].MaximumQty = 0;
    }
}

// CopyItems function used to copy item to database
int CopyItems(items data[])
{
    signed int index = -1;
    
    // Open and read file FoodDB.txt
    FILE *buffer;
    buffer = fopen("FoodDB.txt","r");
    
    // A condition for program to read the databse. Each data we separate by #
    while(!feof(buffer))
    {
        index++;
        fscanf(buffer,
               "%[^#]#%[^#]#%d#%d#%d#%d\n",
               &data[index].ItemsCode,
               &data[index].ItemsName,
               &data[index].Price,
               &data[index].Qty,
               &data[index].MinimumQty,
               &data[index].MaximumQty);
    }
    fclose(buffer);
    return index+1;
}

// WriteFile function used to write file to database
void WriteFile(items data[], int amount)
{
    unsigned int index = 0;
    
    // Open and write file FoodDB.txt
    FILE *buffer;
    buffer = fopen("FoodDB.txt","w");
    
     // A condition for program to write the databse. Each data we separate by #
    for(index = 0; index < amount; index++)
    fprintf(buffer,
            "%s#%s#%d#%d#%d#%d\n",
            data[index].ItemsCode,
            data[index].ItemsName,
            data[index].Price,
            data[index].Qty,
            data[index].MinimumQty,
            data[index].MaximumQty);
    
    fclose(buffer);
}

// PrintItems function used to show or print items in database
void PrintItems(items data[], int amount)
{
    unsigned int index;
    
    // Print data to the screen
    printf("\nNo. %-8s %-29s %3s %3s %3s %4s %s\n",
           "Code",
           "Items Name",
           "Price",
           "Qty",
           "Min",
           "Max",
           "Status");
    
    // Statement for looping data from the beginning until the end of data       
    for(index = 0; index < amount; index++)
    {
        printf("%2d. %-8s %-29s %5d %3d %3d %4d",
               index+1,
               data[index].ItemsCode,
               data[index].ItemsName,
               data[index].Price,
               data[index].Qty,
               data[index].MinimumQty,
               data[index].MaximumQty);
        
        // Item qty status, if the qty below the minimum qty, the screen displayed "Order" message, if not "Ok" message        
        if(data[index].Qty <= data[index].MinimumQty) printf(" Order\n");
        else printf(" Ok\n");
    }
}

// InputItems function used to input item from user that will be written in database when user call WriteFile function
void InputItems(items data[], int *amount)
{
    char choice ='y';
    
    // Condition while user input item data, if user want to input data again, then type y or Y and the program will displayed list below again
    while(choice == 'y')
    {
        clrscr();
        printf("Input New Item Into Database\n");
        fflush(stdin);
        printf("Item Code     : "); scanf("%s", data[*amount].ItemsCode);
        printf("Item Name     : "); scanf("%s", data[*amount].ItemsName);
        printf("Price         : "); scanf("%d", &data[*amount].Price);
        printf("Qty           : "); scanf("%d", &data[*amount].Qty);
        printf("Minimum Stock : "); scanf("%d", &data[*amount].MinimumQty);
        printf("Maximum tock  : "); scanf("%d", &data[*amount].MaximumQty);
        printf("\n\nWant to enter data again [Y/T] ?"); scanf("%c", &choice); getche();
        
        if (choice == 'y') (*amount)++;
    }
    (*amount)++;
}

// ChangeItems function used to change old item data in database to new data
void ChangeItems(items data[], int amount)
{
    unsigned int index,
                 Price,
                 Qty,
                 MinimumQty,
                 MaximumQty;

    char choice = 'y',
         ItemsCode[6],
         ItemsName[31];
    
    clrscr();
    printf("Change Item In Database\n");
    printf("OLD DATA\n"); fflush(stdin);
    printf("Item Code : ");  // User input the item code that will be changed
    scanf("%s", ItemsCode);
     
    for(index = 0; index < amount; index++)
    {
        if(strcmp(data[index].ItemsCode, ItemsCode) == 0)
        break;
    }
    
    // Program shown data from item code that user inputted before
    printf("Item Code     : %s\n", data[index].ItemsName);
    printf("Price         : %d\n", data[index].Price);
    printf("Qty           : %d\n", data[index].Qty);
    printf("Minimum Stock : %d\n", data[index].MinimumQty);
    printf("Maximum Stock : %d\n", data[index].MaximumQty);
    
    printf("\nNEW DATA\n");
    fflush(stdin);
    // User input new data from item code that user inputted before
    printf("Item Code     : "); scanf("%s", ItemsCode);
    printf("Item Name     : "); scanf("%s", ItemsName);
    printf("Price         : "); scanf("%d", &Price);
    printf("Qty           : "); scanf("%d", &Qty);
    printf("Minimum Stock : "); scanf("%d", &MinimumQty);
    printf("Maximum Stock : "); scanf("%d", &MaximumQty);
    printf("\n\nIs the new data correct [Y/T] ?"); choice = getche();
    
    // When user press Y, then the program will replaced old data by new data
    if(choice=='y')
    {
        strcpy(data[index].ItemsCode, ItemsCode);
        strcpy(data[index].ItemsName, ItemsName);
        data[index].Price = Price;
        data[index].Qty = Qty;
        data[index].MinimumQty = MinimumQty;
        data[index].MaximumQty = MaximumQty;
    }
}

// Title function displayed program title
void Title()
{
     // Program title
     printf("======================================\n");
     printf("            KANTIN KENYANG            \n");
     printf(" Jl. Maju Terus Pantang Mundur No. 45 \n");
     printf("         Jakarta Pusat 12000          \n");
     printf("======================================  ");
     printf("\n\n");
}
 
int main()
{
    items data[1000];
    
    int stat = 1,
        amount,
        choice;
    
    DeleteItems(data);
    amount = CopyItems(data);
    
    while(stat)
    {
        clrscr();
        Title(); // call the Title function
        printf("MENU\n");       

        printf(" 1. Show Items\n");
        printf(" 2. Input New Item\n");
        printf(" 3. Change Item Data\n");
        printf(" 0. Exit\n\n");
        
        printf("Choice : "); scanf("%d", &choice);
        
        // Statement that will be selected by user
        switch(choice)
        {
            case 1: PrintItems(data, amount);
                    stat = 1;
                    printf("\nPress any key to continue");
                    getch();
                    break;
            case 2: InputItems(data, &amount);
                    stat = 1;
                    getch();
                    break;
            case 3: ChangeItems(data, amount);
                    stat = 1;
                    getch();
                    break;
            case 0: WriteFile(data, amount);
                    stat = 0;
                    return 0;
            default : stat = 1;
                      break;
        }
    }
    
    getch();
    return 0;
}

0 komentar:

Post a Comment