Comments | Online Safety | Help
     
 

Other files that are needed to run this program - amend.dat and stock.dat

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

void query_files (FILE *new_stock_file, FILE *query_file);
void amend_details (char type, int *qty, int *buy, int *sell, int value);

int main ()
{
    FILE *stock_file = NULL,
            *amend_file = NULL,
            *query_file = NULL,
            *new_stock_file = NULL;

    int code = -1,
        qty = 0,
        buy = 0,
        sell = 0,
        amend = 0,
        value = 0,
        pre_amend = 0;

    char type = NULL,
    pre_type = NULL;

    /*Open files ready for processing*/
    if ( (stock_file = fopen("stock.dat","rt")) == NULL || /*Read*/
        (amend_file = fopen("amend.dat","rt")) == NULL || /*Read*/
        (new_stock_file = fopen("new_stock.dat","wt")) == NULL ) /*Write*/
    {
        printf("Error in Opening Files for Amendment Processing.\n");
        exit(EXIT_FAILURE);
    }

    /*Get amendment file details*/
    fscanf(amend_file,"%d,%c,%d",&amend,&type,&value);

    /*Store values needed for writing to new stock file*/
    pre_amend = amend;
    pre_type = type;

    /*Process files until amendment code is equal to 9999 */
    while(amend != 9999)
    {
        /*Get next line from stock file while the product code is less than */
        /*that of the amend files product code. */
        while ( code < amend )
        {
            fscanf(stock_file,"%d,%d,%d,%d",&code,&qty,&buy,&sell);
            /*If no amendments to current product, add product to new file */
            if (code != amend && code >= 0)
                fprintf(new_stock_file,"%d,%d,%d,%d\n",code,qty,buy,sell);
        }

        /* Amend stock file values */
        amend_details(type,&qty,&buy,&sell,value);

        /*Get next amendment file details*/
        fscanf(amend_file,"%d,%c,%d",&amend,&type,&value);

        /*If there are no more amendments for the current product then write */
        /*the new values to the new stock file */
        if (amend != pre_amend)
        {
            if (pre_type != 'D') /*Do not write a product marked for deletion*/
                fprintf(new_stock_file,"%d,%d,%d,%d\n",code,qty,buy,sell);
            pre_type = type;
            pre_amend = amend;
        }
    }

    /*Add the remainder of stock file to new file*/
    while ( code != 9999 )
    {
        fscanf(stock_file,"%d,%d,%d,%d",&code,&qty,&buy,&sell);
        if (code == 9999)
            fprintf(new_stock_file,"9999\n");
        else
            fprintf(new_stock_file,"%d,%d,%d,%d\n",code,qty,buy,sell);
    }

    /* Close files after processing */
    fclose(stock_file);
    fclose(amend_file);
    fclose(new_stock_file);

    /*Open files ready for processing */
    if ( (query_file = fopen("query.dat","wt")) == NULL || /*Write*/
        (new_stock_file = fopen("new_stock.dat","rt")) == NULL ) /*Read*/
    {
        printf("Error in Opening Files for Query Processing.\n");
        exit(EXIT_FAILURE);
    }

    /* Query file - Check if any stock item is outside the given range */
    query_files(new_stock_file, query_file);

    /* Close files after processing */
    fclose(query_file);
    fclose(new_stock_file);

    return EXIT_SUCCESS;
}

void amend_details (char type, int *qty, int *buy, int *sell, int value)
{
    switch (type)
    {
        case 'I':
            /* Process Amendment - Issue from stock.*/
            *qty -= value;
            break;
        case 'R':
            /* Process Amendment - Receipt into stock.*/
            *qty += value;
            break;
        case 'B':
            /* Process Amendment - Buying price.*/
            *buy += value;
            break;
        case 'S':
            /* Process Amendment - Sale price.*/
            *sell += value;
            break;
        case 'D':
            /* Do not write current entry to new stock file */
            /* Process Amendment - Delete stock item.*/
            break;
        default:
            printf("Undefinded Amendment!! Program Terminated.\n");
            exit(1);
    }
}

void query_files (FILE *new_stock_file, FILE *query_file)
{
    int code = -1,
    qty = 0,
    buy = 0,
    sell = 0;

    fscanf(new_stock_file,"%d,%d,%d,%d",&code,&qty,&buy,&sell);

    while (code != 9999)
    {
        if (qty < 1000 || qty > 9999)
            fprintf(query_file,"%d,%d,%d,%d\n",code,qty,buy,sell);
        fscanf(new_stock_file,"%d,%d,%d,%d",&code,&qty,&buy,&sell);
    }
}