Code:
#include <stdio.h>
#include <stdlib.h>
void query_files (FILE *new_stock_file, FILE *query_file);
void amend_details (char type, int *amount, int *buy_price, int *sell_price, int value);
int main ()
{
FILE *stock_file = NULL,
*amend_file = NULL,
*query_file = NULL,
*new_stock_file = NULL;
int code = -1,
amount = 0,
buy_price = 0,
sell_price = 0,
amend = 0,
value = 0,
amend_2 = 0;
char type = NULL,
type_2 = 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(1);
}
/*Get amendment file details*/
fscanf(amend_file,"%d,%c,%d",&amend,&type,&value);
/*Store values needed for writing to new stock file*/
amend_2 = amend;
type_2 = 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,&amount,&buy_price,&sell_price);
/*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,amount,buy_price,sell_price);
}
/* Amend stock file values */
amend_details(type,&amount,&buy_price,&sell_price,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 != amend_2)
{ if (type_2 != 'D') /*Do not write a product marked for deletion*/
fprintf(new_stock_file,"%d,%d,%d,%d\n",code,amount,buy_price,sell_price);
type_2 = type;
amend_2 = amend;
}
}
/*Add the remainder of stock file to new file*/
while ( code != 9999 )
{ fscanf(stock_file,"%d,%d,%d,%d",&code,&amount,&buy_price,&sell_price);
if (code == 9999)
fprintf(new_stock_file,"9999\n");
else
fprintf(new_stock_file,"%d,%d,%d,%d\n",code,amount,buy_price,sell_price);
}
/* 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(1);
}
/* 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 *amount, int *buy_price, int *sell_price, int value)
{ switch (type)
{ case 'I':
/* process Issue from stock.*/
*amount = (*amount - value);
break;
case 'R':
/* process Receipt to stock.*/
*amount = *amount + value;
break;
case 'B':
/* process Buying price amendment.*/
*buy_price = *buy_price + value;
break;
case 'S':
/* Process Amendment - Sale price.*/
*sell_price = *sell_price + value;
break;
case 'D':
/* Do not write current entry to new stock file */
/* Process Amendment - Delete stock item.*/
break;
default:
printf("Illegal Amendment Character Program Ending.\n");
exit(EXIT_FAILURE);
}
}
void query_files (FILE *new_stock_file, FILE *query_file)
{ int code = -1,
amount = 0,
buy_price = 0,
sell_price = 0;
fscanf(new_stock_file,"%d,%d,%d,%d",&code,&amount,&buy_price,&sell_price);
while (code != 9999)
{ if (amount < 1000 || amount > 9999)
fprintf(query_file,"%d,%d,%d,%d\n",code,amount,buy_price,sell_price);
/* Write to query file if product code is out of range */
fscanf(new_stock_file,"%d,%d,%d,%d",&code,&amount,&buy_price,&sell_price);
}
}