[Solved] C Programming Rewrite Sequential Access File Delete Update Function Ask User Input Several Q37219908
C programming :How do I rewrite this into a sequential accessfile (no delete or update function. You will ask the user to inputseveral items and then write a sequential access file of the data.Then close and reopen the file to print the records.) ?
#include
#include
//Define the structure to store required information for
//the hardware inventory.
struct hardwareInventoryData{
//Declare required variables to store informationof
//the tool record.
unsigned int rec_num;
char name_of_tool[31];
unsigned int num_quantity;
double cost_of_tool;
};
//Declare the prototypes of the functions goingto
//be used.
unsigned int choiceMenu(void);
void appendRecordsToFile(FILE *rptr);
void addARecordToFile(FILE *filePtr);
void displayRecord(FILE *filePtr);
int main(){
//Delcare required file pointers.
FILE *datafilePtr;
FILE *createFilePtr;
//Declare and initialize the structure typevariable.
struct hardwareInventoryData initialTools ={ 0, “”,0, 0.0 };
//Start the while loop till the file pointer toopen
//the hardware.dat file is null.
while((datafilePtr = fopen(“hardware.dat”, “r+b”))==NULL){
//If the file pointer is null, then open the
//hardware.dat file again in write mode.
//If this file pointer is also null, then file can
//not be open.
if((createFilePtr = fopen(“hardware.dat”, “rb+”)) == NULL){
puts(“This File can not be open.n” );
}
else{
//Start the for loop from 1 to 100.
int index;
for(index = 1; index <= 100; index++ ){
//Initialize 100 empty recordsin the file
//hardware.dat.
fwrite(&initialTools, sizeof( struct hardwareInventoryData), 1, createFilePtr);
}
//Close the file pointer.
fclose(createFilePtr);
} // end for
} // end else
//If the file pointer to open the filehardware.dat
//is still null, then file can not be open.
if((datafilePtr = fopen(“hardware.dat”, “r+b”))==NULL){
puts(“File can not be open.n”);
}
else{
//Declare a variable to store menu choice.
unsigned int ch;
//Start the while loop till the user choice is not 9.
while((ch = choiceMenu()) != 9){
//Start the switch case over the user’schoice.
switch (ch){
//Call the function appendRecordsToFile() to
//write the tool records to the text file by
//passing the file pointer used to open the
//file hardware.dat.
case 1:
appendRecordsToFile(datafilePtr);
break;
//Define case 2 by calling the function
//addARecordToFile() to add a record in the
//file by passing the file pointer used to
//open the file hardware.dat.
case 2:
addARecordToFile(datafilePtr);
break;
//Define case 3 by calling the function
//displayRecord() to display a record by
//passing the file pointer used to open
//the file hardware.dat.
case 3:
displayRecord(datafilePtr);
break;
//Display an appropriate message in the defautcase.
default:
puts(“Invalid choice!”);
break;
} // end switch
}// end while
//Close the file.
fclose(datafilePtr);
} // end else
} // end main
//Define the function
void appendRecordsToFile(FILE *rptr){
//Declare a file pointer variable to open a textfile.
FILE *outFile;
//Open the file hardware.txt file in the writemode.
//If the file pointer is null, then file cannot beopen.
if((outFile = fopen(“hardware.txt”, “w”)) ==NULL){
puts(“File cannot be open.”);
}
else{
//Rewind the file pointer to the beginning of the
//text file using rewind() function.
rewind(rptr);
//Write the header of the table to store the
//hardware inventory records.
fprintf(outFile, “%-10s %-20s%-10s%-10sn”,
“Record #”, “Tool Name”, “Quantity”, “Cost”);
//Start the while loop till the end of file is not reached.
while(!feof(rptr)){
//Create a variable of tructure type.
struct hardwareInventoryData initialTools ={0, “”,0, 0.0};
//Read the input file hardware.dat usingfread()
//and store the result into a variable.
int res = fread(&initialTools, sizeof(structhardwareInventoryData), 1, rptr);
//If the resultant value is not 0 and record number isnot 0.
if(res != 0 && initialTools.rec_num !=0){
//Write the required information stored in the
//structure variable to the file.
fprintf(outFile, “%-8d %-20s %-10d %-10fn”,
initialTools.rec_num,
initialTools.name_of_tool,
initialTools.num_quantity,
initialTools.cost_of_tool);
} // end if
} // end while
//Close the text file.
fclose(outFile);
} // end else
} // end function appendRecordsToFile
//Define the function addARecordToFile().
void addARecordToFile(FILE *filePtr){
//create an empty rstructure record
struct hardwareInventoryData tool_rec ={ 0, “”, 0, 0.0};
//Prompt the user to enter a record number.
printf(“Enter a record number ( 1 – 100, 0 to return”
“to main menu): “);
scanf(“%d”, &tool_rec.rec_num );
//If the record number is not between 0-100, thendisplay an appropriate essage.
if(tool_rec.rec_num < 0 || tool_rec.rec_num>100)
printf(“It is an invalid record number.n”);
else{
//Start the while loop till the record number entered by theuser is not 0.
while(tool_rec.rec_num != 0){
//Prompt the user to enter the tool name,quantity,and cost.
printf(“Enter the name of the tool: “);
//Get the user input till the next line isentered.
while (getchar() != ‘n’);
//Store the input as the name of the tool.
fgets(tool_rec.name_of_tool, 30, stdin);
//Declare a variable to store the last index of the name of thetool.
int lindex;
//Calculate the last index by getting 1 minus
//the length of the name of the tool.
lindex = strlen(tool_rec.name_of_tool)-1;
//If the character at the last index is new
//linecharacter, then replace it with
//character.
if (tool_rec.name_of_tool[lindex] == ‘n’)
tool_rec.name_of_tool[lindex] = ”;
printf(“Enter the quantity of the tool: “);
scanf(“%d”, &tool_rec.num_quantity);
printf(“Enter the cost of tool: ” );
scanf(“%lf”, &tool_rec.cost_of_tool);
//Set the file pointer to the required record in
//the file.
fseek(filePtr, (tool_rec.rec_num – 1) *
sizeof(struct hardwareInventoryData), SEEK_SET);
//Write the required information to the file.
fwrite(&tool_rec, sizeof(struct
hardwareInventoryData ), 1, filePtr);
//Again, prompt the user to enter the record
//number.
printf(“nEnter another record number, enter “
“0 to exit: ” );
scanf(“%d”, &tool_rec.rec_num );
}
}
}
//Define the function displayRecord().
void displayRecord(FILE *filePtr)
{
//Declare required variable.
unsigned int rnum;
//Prompt the user to enter a record number to display
//that record.
printf(“%s”, “Enter a record number to display “
“(1 – 100): “);
scanf(“%d”, &rnum);
//Set the file pointer to the required record in the
//file.
fseek(filePtr, (rnum – 1) * sizeof(struct
hardwareInventoryData), SEEK_SET);
//Declare the tructure type variable.
struct hardwareInventoryData tool_rec =
{0, “”, 0, 0.0};
//Read the required record from the file using
//fread().
fread(&tool_rec, sizeof(struct
hardwareInventoryData), 1, filePtr);
//If the record number is 0, then display an
//appropriate message.
if(tool_rec.rec_num == 0)
{
printf(“Record #%d does not exist.n”, rnum);
}
//Otherwise.
else
{
//Set the file pointer to the required record in the
//file.
fseek(filePtr, (rnum – 1) * sizeof(struct
hardwareInventoryData), SEEK_SET);
//If the record number is not 0, then display all
//the information of the tool.
if(tool_rec.rec_num != 0 )
{
printf(“%-10d %-20s %-10d %-10fn”,
tool_rec.rec_num, tool_rec.name_of_tool,
tool_rec.num_quantity,
tool_rec.cost_of_tool);
}
//Otherwise, display an appropriiate message.
else
{
printf(“This record does not exist!n”);
}
}
}
//Define the function choiceMenu().
unsigned int choiceMenu()
{
//Display the menu of choices.
printf(“%s”, “nYour choices are:n”
“1 – STRORE a formatted text file of hardware”
” calledn”
” “hardware.txt” for printingn”
“2 – UPDATE a recordn”
“3 – ADD a new recordn”
“4 – DELETE a recordn”
“5 – DISPLAY a tool recordn”
“9 – END programn Selection : “);
//Declare required variable to store the user’s
//choice.
unsigned int user_choice;
scanf(“%d”, &user_choice);
//Return the user’s choice.
return user_choice;
}
Expert Answer
Answer to C programming :How do I rewrite this into a sequential access file (no delete or update function. You will ask the user … . . .
OR

