Hi!! Please note that it needs to be coded in C89 and it cannot include the stri
ID: 3915331 • Letter: H
Question
Hi!! Please note that it needs to be coded in C89 and it cannot include the string library function. Thank you!
Project [9]: File Input/output
Project Goals
The goal of this project are to:
1.Get students familiar with functions to read from and write to files, in text and binary modes
Important Notes:
1.Formatting:
Make sure that you follow the precise recommendations for the output content and
formatting: for example, do not change the text in the first problem from
“Please enter item data (part number, quantity, price): ” to “Enter the item: ”. Your assignment willbe auto-graded and any changes in formatting will result in a loss in the grade.
2.Comments:
Header comments are required on all files and recommended for the rest of the program.
Points will be deducted if no header comments are included.
3.Restriction: The use of goto statements anywhere within this program is prohibited. Points will be deducted if goto is used.
Problem 1
Write a program to repeatedly ask the user to enter information regarding inventory for a business (item part number, quantity, price) and then saves the information to a file called
inventory.txt.
The program stops the loop when the user enters 0 for the part number.
The program should function as follows (items underlined are to be entered by the user):
This program stores a business inventory.
Please enter item data (part number, quantity, price):
3, 1, 2.4
Please enter item data (part number, quantity, price):
1, 4, 3.0
Please enter item data (part number, quantity, price):
0
Thank you. Inventory stored in file inventory.txt.
Note:
?Your program must write to the file in binary mode, using the fwrite
Function. Save your program as save_inventory.c
Modify your program to check if the user enters an identical part number again. If this occurs, the program should print a message and ask for the input again.
The program should function as follows (items underlined are to be entered by the user):
This program stores a business inventory.
Please enter item data (part number, quantity, price):
3, 1, 2.4
Please enter item data (part number, quantity, price):
1, 4, 3.
0
Please enter item data (part number, quantity, price):
3, 4, 3.0
This item has been entered before.
Please enter item data (part number, quantity, price):
2, 4, 1.3
Please enter item data (part number, quantity, price): 0
Thank you. Inventory stored in file inventory.txt.
Problem 2
Write a program to read information from the
inventory.txt
file and display it to the screen, formatted as follows: Part#, Quantity, and Item Price in the table header should be separated by tabs. The part number field should take 5 spaces (values right justified), the quantity field should take 8 spaces (values right justified), and the price field should take 9 spaces with 2 numbers after the decimal (values right justified, with the $ sign in front of the price). The program should function as follows:
Below are the items in your inventory.
Part# Quantity Item Price
3 1 $ 2.40
1 4 $ 3.00
2 4 $
1.30
Note:
?Your program must read from the file in binary mode, using the fread function
Save your program as disp_inventory.c
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
// Defines a structure to store item information
struct Item
{
// Data member to store data
int partNumber;
int quantity;
float price;
};// End of structure definition
// Function to search a part number for duplicate
// If duplicate part number found return the index position otherwise return -1
int findDuplicate(int partNo[], int len, int partNumber)
{
// Loops variable
int c;
// Loops till length of the part number array
for(c = 0; c < len; c++)
{
// Checks if the current index position of the part number array value is equals to the part number passed as parameter
if(partNo[c] == partNumber)
// Returns the c value as found index position
return c;
}// End of for loop
// Otherwise returns -1
return -1;
}// End of function
// Function to write data to file using binary mode
void writeData()
{
// Loops variable and index counter
int n, c = 0;
// Declares a FILE pointer
FILE *fptr;
// Declares an Item object
struct Item item;
// Declares an array of size MAX to store the part numbers
int partNo[MAX];
// Checks if file cannot be opened in write mode then display error message and exit the program
if ((fptr = fopen("inventory.txt","wb")) == NULL)
{
printf("Error! Opening file for writing.");
// Program exits if the file pointer returns NULL.
exit(1);
}// End of if condition
// Loops till user enters 0 as part number
do
{
// Displays the message
printf(" Enter 0 for the part number to stop.");
printf(" Please enter item data (part number, quantity, price): ");
// Accepts part number
scanf("%d", &item.partNumber);
// Checks if part number entered is zero then come out of the loop
if(item.partNumber == 0)
break;
// Otherwise accept rest data and write data to file
else
{
// Accepts the quantity and price
scanf("%d %f", &item.quantity, &item.price);
// Calls the function to check duplicate part number
int status = findDuplicate(partNo, c, item.partNumber);
// Checks if return value is -1 then no duplicate
if(status == -1)
{
// Adds the part number entered by the user to array c index position and increase the index counter by one
partNo[c++] = item.partNumber;
// Writes the object to file
fwrite(&item, sizeof(struct Item), 1, fptr);
}// End of if condition
// Otherwise duplicate part number found
else
printf(" This item has been entered before.");
}// End of else
}while(1); // End of while loop
// Closes the file
fclose(fptr);
}// End of function
// Function to read data from file
void readData()
{
// Loop variable
int n;
// Declares an object of Item
struct Item item;
// Declares a FILE pointer
FILE *fptr;
// Checks if file can be opened in read mode
if ((fptr = fopen("inventory.txt","rb")) == NULL)
{
// Displays error message
printf("Error! Opening file for reading.");
// Program exits if the file pointer returns NULL.
exit(1);
}// End of if condition
// Displays heading
printf(" %5s %8s %9s", "Part#", "Quantity", "Item Price ");
//printf(" Part# Quantity Item Price ");
// Loops till end of the file
while(!feof(fptr))
{
// Reads the file data and stores it in Item object
fread(&item, sizeof(struct Item), 1, fptr);
// Displays the data
printf("%-5d %-8d $%-9.2f ", item.partNumber, item.quantity, item.price);
}// End of while loop
// Closes the file
fclose(fptr);
}// End of function
// main function definition
int main()
{
// Calls the function to write data to file
writeData();
// Calls the function to read data from file
readData();
}// End of main function
Sample Output:
Enter 0 for the part number to stop.
Please enter item data (part number, quantity, price): 1
2
3.6
Enter 0 for the part number to stop.
Please enter item data (part number, quantity, price): 2
5
6.3
Enter 0 for the part number to stop.
Please enter item data (part number, quantity, price): 2
3
9.2
This item has been entered before.
Enter 0 for the part number to stop.
Please enter item data (part number, quantity, price): 3
4
8.9
Enter 0 for the part number to stop.
Please enter item data (part number, quantity, price): 0
Part# Quantity Item Price
1 2 $3.60
2 5 $6.30
3 4 $8.90
3 4 $8.90
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.