C not c not java Hardware Inventory Write a database to kee
C not c++ not java
Hardware Inventory – Write a database to keep track of tools, their cost and number. Your program should initialize hardware.dat to 100 empty records, let the user input a record number, tool name, cost and number of that tool. Your program should let you delete and edit records in the database. The next run of the program must start with the data from the last session.
Example Program Session:
(First Run)Enter request
1 - Input new tool or update an existing tool
2 - Delete a tool
3 - List all tools
4 - Exit
? 3
Record # Tool name Quantity Cost
Enter request
1 - Input new tool or update an existing tool
2 - Delete a tool
3 - List all tools
4 - Exit
? 1
Enter record number ( 1 to 100, 0 to return to main menu )
? 5
Enter tool name, quantity, cost
? saw 102 12
Enter record number ( 1 to 100, 0 to return to main menu )
? 7
Enter tool name, quantity, cost
? hammer 75 8
Enter record number ( 1 to 100, 0 to return to main menu )
? 0
Enter request
1 - Input new tool or update an existing tool
2 - Delete a tool
3 - List all tools
4 - Exit
? 3
Record # Tool name Quantity Cost
5 saw 102 12.00
7 hammer 75 8.00
Enter request
1 - Input new tool or update an existing tool
2 - Delete a tool
3 - List all tools
4 - Exit
? 4
Press any key to continue . . .
(Second Run)Enter request
1 - Input new tool or update an existing tool
2 - Delete a tool
3 - List all tools
4 - Exit
? 3
Record # Tool name Quantity Cost
5 saw 102 12.00
7 hammer 75 8.00
Enter request
1 - Input new tool or update an existing tool
2 - Delete a tool
3 - List all tools
4 - Exit
? 4
Press any key to continue …
Solution
#include <stdio.h>
struct toolRec
{
int recNum;
char name[15];
int quant;
double cost;
};
int menu( void );
void inputRec( FILE *fPtr );
void deleteRec( FILE *fPtr );
void printAll( FILE *readPtr );
int main()
{
int choice;
FILE *rfPtr;
FILE *wfPtr;
struct toolRec initTool = { 0, \"\", 0, 0.0 };
while ( ( rfPtr = fopen( \"records.dat\", \"r+b\" ) ) == NULL )
{
if ( ( wfPtr = fopen( \"records.dat\", \"wb\" ) ) == NULL )
{
printf( \"File could not be opened.\ \" );
}
else
{
for ( int i = 1; i <= 100; i++ )
{
fwrite( &initTool, sizeof( struct toolRec ), 1, wfPtr );
} fclose( wfPtr );
}
}
if ( ( rfPtr = fopen( \"records.dat\", \"r+b\" ) ) == NULL )
{
printf( \"File could not be opened.\ \" );
}
else
{
while( ( choice = menu() ) != 4 )
{
switch ( choice )
{
case 1:
inputRec( rfPtr );
break;
case 2:
deleteRec( rfPtr );
break;
case 3:
printAll( rfPtr );
break;
default:
printf( \"Incorrect Choice.\ \" );
break;
}
}
}
fclose(rfPtr);
return 0;
}
int menu( void )
{
int request;
printf( \"\ Enter your request:\ \" \"1 - Input new tool or update an existing tool\ \"
\"2 - Delete a tool\ \"
\"3 - List all tools\ \"
\"4 - Exit\ ? \" );
scanf( \"%d\", &request );
return request;
}
void inputRec( FILE *fPtr )
{
struct toolRec tool = { 0, \"\", 0, 0.0 };
printf( \"Enter record number\"
\" ( 1 - 100, 0 to return to main menu)\ ? \" );
scanf( \"%d\", &tool.recNum );
if( tool.recNum < 0 || tool.recNum > 100 )
printf( \"Invalid record number.\ \" );
else
{
while( tool.recNum != 0 )
{
printf( \"Enter the tool\'s name, \"
\"cost and quantity.\ ? \" );
scanf( \"%s%d%lf\", tool.name, tool.quant, &tool.cost );
fseek( fPtr, ( tool.recNum - 1 ) *
sizeof( struct toolRec ), SEEK_SET );
fwrite( &tool, sizeof( struct toolRec ), 1, fPtr );
printf( \"Enter another request:\ ? \" );
scanf( \"%d\", &tool.recNum );
}
}
}
void deleteRec( FILE *fPtr )
{
struct toolRec tool = { 0, \"\", 0, 0.0 };
printf( \"Enter record number to delete\"
\" (1 - 100, 0 to return to main menu)\ ? \" );
scanf( \"%d\", &tool.recNum );
if( tool.recNum >= 1 && tool.recNum <= 100 ) {
fseek( fPtr, ( tool.recNum - 1 ) *
sizeof( struct toolRec ), SEEK_SET );
fwrite( &tool, sizeof( struct toolRec ), 1, fPtr );
} else if( tool.recNum < 0 || tool.recNum > 100 )
printf( \"Invalid record number.\ \" );
}
void printAll( FILE *readPtr )
{
struct toolRec tool = { 0, \"\", 0, 0.0 };
rewind( readPtr );
printf( \"%-10s%-20s%-10s%-10s\", \"Record #\", \"Tool Name\"
\"Quantity\", \"Cost\ \" );
while( !feof( readPtr ) ) {
fread( &tool, sizeof( struct toolRec ), 1, readPtr );
if( tool.recNum != 0 ) {
printf( \"%-10d%-20s%-10d%-10lf\",
tool.recNum, tool.name,
tool.quant, tool.cost );
}
}
}





