Code:
/* Tool Inventory List */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*toolData structure definition */
struct toolData {
int toolNum; /* tool record number */
char name[25]; /* tool name */
int quantity; /* number of tools */
double cost; /* tool cost */
}; /* end struct toolData */
/*prototypes */
int enterChoice( void );
void textFile( FILE *readPtr );
void updateName( FILE *fPtr );
void updateQuantity ( FILE *fPtr);
void updateCost ( FILE *fPtr);
void updateNumber (FILE *fPtr);
void createNew( FILE *fPtr );
void deleteRecord( FILE *fPtr );
int main (void){
FILE *tdPtr; /* tool.dat file pointer */
int choice; /* user choice */
int i; /* counter */
/* create toolData with default information */
struct toolData blankTool = {0, "", 0, 0.0};
/*fopen opens the filel exits if file can't be opened */
if ((tdPtr = fopen ("tool.dat", "wb")) == NULL){
printf("File could not be opened.\n");
} /* end if */
else{
/*output 100 blank records to file */
for (i=1; i<=100; i++){
fwrite( &blankTool, sizeof( struct toolData),1, tdPtr);
} /* end for */
fclose (tdPtr); /* closes the file */
}/* end else */
/* fopen opens the file; exits of file can't be opened */
if ( ( tdPtr = fopen( "tool.dat", "rb+" ) ) == NULL ){
printf( "File can not be opened.\n");
} /* end if */
else{
/*enable user directed action */
while (( choice = enterChoice() ) != 8) {
switch (choice){
case 1: /* create text file from record file */
textFile( tdPtr );
break;
case 2: /* update name*/
updateName ( tdPtr );
break;
case 3: /* update quantity*/
updateQuantity (tdPtr);
break;
case 4:
updateCost (tdPtr); /* update cost */
break;
case 5:
updateNumber (tdPtr); /* update record number */
break;
case 6: /* create new record */
createNew (tdPtr);
break;
case 7: /* delete record */
deleteRecord (tdPtr);
break;
default: /* if invalid choice is entered */
printf( "Sorry, incorrect choice \n");
break;
} /* end switch */
} /* end while */
fclose( tdPtr ); /* closes the file */
} /* end else */
system ("PAUSE");
return 0;
} /* end main */
/* program to allow user menu choice */
int enterChoice (void){
int menuChoice; /* choice variable */
/* display options */
printf(" \nEnter your choice\n"
"1...Store a formatted text file of tool data called\n"
" \"inventory.txt\" for printing.\n"
"2...Update a tool name in record.\n"
"3...Update a tool quantity in record.\n"[/FONT]
"4...Update a tool cost in record.\n"
"5...Update a tool record number in file.\n"
"6...Add a new tool record.\n"
"7...Delete a tool from inventory.\n"
"8...EXIT Program.\n? ");
scanf("%d", &menuChoice);
return menuChoice;
} /* end enterChoice program */
/* 1...Create text file for printing */
void textFile( FILE *readPtr){
FILE *writePtr; /* inventory.txt file pointer */
/* create toolData with default info */
struct toolData tool = { 0, "", 0, 0.0 };
/* opens file; exits if file can't be opened */
if ((writePtr = fopen( "inventory.txt", "w")) == NULL) {
printf("File could not be opened.\n");
} /* end if */
else {
rewind (readPtr); /* sets pointer location to beginning of file */
fprintf( writePtr, "%-6s%-16s%-11s%10s\n", "Record #", "Tool Name", "Quantity", "Cost");
/* copy all records from random-access to text file */
while ( !feof( readPtr )) {
fread( &tool, sizeof( struct toolData ), 1, readPtr);
/* write single record to text file */
if (tool.toolNum != 0) {
fprintf( writePtr, "%-6d%-16s%-11d%10.2f\n", tool.toolNum, tool.name, tool.quantity, tool.cost);
} /* end if */
} /* end while */
fclose( writePtr ); /* closes the file */
} /* end else */
} /* end textFile */
/*2...Update a tool NAME in inventory */
void updateName ( FILE *fPtr ){
int record; /* record number */
char newName[25]; /* new name */
/* create toolData with no information */
struct toolData tool = { 0, "", 0, 0.0 };
/* obtain number of tool to update */
printf("Enter tool to update (1-100): \n");
scanf( "%d", &record );
/* move file pointer to correct record in file */
fseek( fPtr, (record - 1) *sizeof (struct toolData), SEEK_SET);
/* read record from file */
fread( &record, sizeof (struct toolData), 1, fPtr);
/* display error if file doesn't exist */
if (tool.toolNum == 0){
printf("Record #%d has no information.\n", record);
} /* end if */
else { /* update NAME */
printf("%-6d%-16s%-11d%10.2f\n", tool.toolNum, tool.name, tool.quantity, tool.cost);
/* request name edit from user */
printf(" Enter new name:\n ");
scanf("%s", newName);
strcpy( tool.name, newName); /* change name */
printf("%-6d%-16s%-11d%10.2f\n", tool.toolNum, tool.name, tool.quantity, tool.cost);
/* move pointer to correct record in file */
fseek( fPtr, (record - 1) *sizeof (struct toolData), SEEK_SET);
/* write an updated record over old record in file*/
fwrite( &record, sizeof( struct toolData), 1, fPtr);
} /* end else */
} /* end updateName */
/*3...Update a tool QUANTITY in inventory */
void updateQuantity ( FILE *fPtr ){
int record; /* record number */
int adjust; /* quantity adjustment */
/* create toolData with no information */
struct toolData tool = { 0, "", 0, 0.0 };
/* obtain number of tool to update */
printf("Enter tool to update (1-100): ");
scanf( "%d", &record );
/* move file pointer to correct record in file */
fseek( fPtr, (record - 1) *sizeof (struct toolData), SEEK_SET);
/* read record from file */
fread( &record, sizeof (struct toolData), 1, fPtr);
/* display error if file doesn't exist */
if (tool.toolNum == 0){
printf("Record #%d has no information.\n", record);
} /* end if */
else { /* update NAME */
printf("%-6d%-16s%-11d%10.2f\n", tool.toolNum, tool.name, tool.quantity, tool.cost);
/* request quantity edit from user */
printf(" Enter shipment (+) or sale (-):\n ");
scanf("%d", &adjust);
tool.quantity += adjust; /* change quantity */
printf("%-6d%-16s%-11d%10.2f\n", tool.toolNum, tool.name, tool.quantity, tool.cost);
/* move pointer to correct record in file */
fseek( fPtr, (record - 1) *sizeof (struct toolData), SEEK_SET);
/* write an updated record over old record in file*/
fwrite( &record, sizeof( struct toolData), 1, fPtr);
} /* end else */
} /* end updateQuantity *