Question

Hardware Inventory – Write a database to keep track of tools, their cost and number. Your...

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.

This is my program so far, when I run the program I keep getting "File Could not be Opened", which I have embeded in the program however I do not see why it is giving my this output immediatly

#include <stdio.h>
#include <math.h>
struct hardwareData
{
int recordNum;
char toolname[20];
int quantity;
double cost;
};
int enterChoice( void );
void textFile( FILE *readPtr );
void updateRecord( FILE *fPtr );
void newRecord( FILE *fPtr );
void deleteRecord( FILE *fPtr );
FILE *hfPtr;
int main()
{
FILE *cfPtr;
int choice;
if ((cfPtr = fopen( "hardware.dat", "rb+" )) == NULL )
{
printf( "File could not be opened.\n" );
}
else
{
while (( choice = enterChoice()) != 5 )
{
switch ( choice )
{
case 1:
textFile( cfPtr );
break;
case 2:
updateRecord( cfPtr );
break;
case 3:
newRecord ( cfPtr );
break;
case 4:
deleteRecord ( cfPtr );
break;
default:
printf( "Incorrect choice\n" );
break;
}
}
fclose( cfPtr );
}
return 0;
}
void textFile( FILE *readPtr )
{
FILE *writePtr;
struct hardwareData hardware = { 0, "", 0, 0.0 };
if ((writePtr = fopen( "hardware.txt", "w" )) == NULL )
{
printf( "File could not be opened.\n" );
}
else
{
rewind( readPtr );
fprintf( writePtr, "%-6s%-16s%-11s%-10s\n","Record #", "Tool name", "Quantity", "Cost" );
while ( !feof( readPtr ) )
{
fread( &hardware, sizeof( struct hardwareData ), 1, readPtr );
if ( hardware.recordNum != 0 )
{
fprintf(writePtr, "%-6d%-16s%-11d%10.2lf\n", hardware.recordNum, hardware.toolname, hardware.quantity, hardware.cost );
}
}
fclose( writePtr );
}
}
void updateRecord ( FILE *fPtr )
{
int record;
struct hardwareData hardware = { 0, "", 0, 0.0 };
printf( "Enter record to update (1-100): ");
scanf( "%d", &record );
fseek( fPtr, ( record - 1 ) * sizeof( struct hardwareData ), SEEK_SET );
fread( &record, sizeof( struct hardwareData ), 1, fPtr );
if ( hardware.recordNum == 0)
{
printf( "Record #%d has no information.\n", record );
}
else
{
printf( "Enter new tool name, quantity,cost \n?" );
scanf( "%s%d%lf", hardware.toolname, &hardware.quantity, &hardware.cost );
fseek( fPtr, ( record - 1 ) * sizeof( struct hardwareData ), SEEK_SET );
fwrite( &hardware, sizeof( struct hardwareData ), 1, fPtr );
}
}
void deleteRecord( FILE *fPtr )
{
struct hardwareData hardware;
struct hardwareData blankRecord = { 0, "", 0,0.0 };
int recordNum;
printf( "Enter record number to delete (1-100): " );
scanf( "%d", &recordNum );
fseek( fPtr, ( recordNum - 1 ) * sizeof( struct hardwareData ), SEEK_SET );
fread( &hardware, sizeof( struct hardwareData ), 1, fPtr );
if ( hardware.recordNum==0 )
{
printf ( "Record %d does not exist.\n", recordNum );
}
else
{
fseek( fPtr, ( recordNum - 1 ) * sizeof( struct hardwareData ), SEEK_SET );
fwrite( &blankRecord, sizeof( struct hardwareData ), 1, fPtr );
}
}
void newRecord( FILE *fPtr )
{
struct hardwareData hardware = { 0, "", 0, 0.0 };
int piece;
printf( "Enter record number to create (1-100) : " );
scanf( "%d", &piece);
fseek( fPtr, (piece - 1) * sizeof( struct hardwareData ), SEEK_SET );
fread( &hardware, sizeof( struct hardwareData ), 1, hfPtr );
if ( hardware.recordNum != 0 )
{
printf( "Record already exists.\n", hardware.recordNum );
}
else
{
printf( "Enter tool name, quantity, cost\n?" );
scanf("%s%d%.2lf", hardware.toolname, &hardware.quantity, &hardware.cost);
hardware.recordNum = piece;
fseek(fPtr, (hardware.recordNum-1) * sizeof( struct hardwareData), SEEK_SET);
fwrite( &hardware, sizeof( struct hardwareData), 1, hfPtr);
}
}
int enterChoice( void )
{
int menuChoice;
printf( "\nEnter your choice\n"
"1 - List of all tools\n"
"2 - Update an existing tool\n"
"3 - Add a new tool\n"
"4 - Delete a tool\n"
"5 - End program\n? " );
scanf( "%d", &menuChoice );
return menuChoice;
}

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Hardware Inventory – Write a database to keep track of tools, their cost and number. Your...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Hardware Inventory – Write a database to keep track of tools, their cost and number. Your...

    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 2 - Delete...

  • Identify and list all the User defined Functions to be used in the system #include <stdio.h>...

    Identify and list all the User defined Functions to be used in the system #include <stdio.h> ///for input output functions like printf, scanf #include <stdlib.h> #include <conio.h> #include <windows.h> ///for windows related functions (not important) #include <string.h> ///string operations /** List of Global Variable */ COORD coord = {0,0}; /// top-left corner of window /** function : gotoxy @param input: x and y coordinates @param output: moves the cursor in specified position of console */ void gotoxy(int x,int y) {...

  • // Write the compiler used: Visual studio // READ BEFORE YOU START: // You are given...

    // Write the compiler used: Visual studio // READ BEFORE YOU START: // You are given a partially completed program that creates a list of patients, like patients' record. // Each record has this information: patient's name, doctor's name, critical level of patient, room number. // The struct 'patientRecord' holds information of one patient. Critical level is enum type. // An array of structs called 'list' is made to hold the list of patients. // To begin, you should trace...

  • Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int...

    Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int isFull(int *, int); void removeAt(int *, int); void printList(int *, int); int main() { int *a; int arraySize=0,l=0,loc=0; int choice; while(1) { printf("\n Main Menu"); printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of...

  • Hello! I'm posting this program that is partially completed if someone can help me out, I...

    Hello! I'm posting this program that is partially completed if someone can help me out, I will give you a good rating! Thanks, // You are given a partially completed program that creates a list of employees, like employees' record. // Each record has this information: employee's name, supervisors's name, department of the employee, room number. // The struct 'employeeRecord' holds information of one employee. Department is enum type. // An array of structs called 'list' is made to hold...

  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

  • I JUST NEED HELP WITH DISPLAY PART! please help! thanks in advance // This function saves...

    I JUST NEED HELP WITH DISPLAY PART! please help! thanks in advance // This function saves the array of structures to file. It is already implemented for you. // You should understand how this code works so that you know how to use it for future assignments. void save(char* fileName) { FILE* file; int i; file = fopen(fileName, "wb"); fwrite(&count, sizeof(count), 1, file); for (i = 0; i < count; i++) { fwrite(list[i].name, sizeof(list[i].name), 1, file); fwrite(list[i].class_standing, sizeof(list[i].class_standing), 1, file);...

  • Programming in C: I am trying to modify this linked list to be doubly linked list....

    Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...

  • URGENT. Need help editing some C code. I have done most of the code it just...

    URGENT. Need help editing some C code. I have done most of the code it just needs the following added: takes an input file name from the command line; opens that file if possible; declares a C struct with three variables; these will have data types that correspond to the data types read from the file (i.e. string, int, float); declares an array of C structs (i.e. the same struct type declared in point c); reads a record from the...

  • Using C, I need help debugging this program. I have a few error messages that I'm...

    Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT