Question: In the previous module, you have implemented product management system of a shop. In this task, you will extend the software with file reading /

In the previous module, you have implemented product management system of a shop. In this task, you will extend the software with file reading/writing features.
A product has three fields: name, price and number of items in stock. Thus, a product is represented as a structure given below.
typedef struct {
char name[31];
float price;
int in_stock;
} Product;
The shop owner desires to store the products as an array of Product elements. The last product in the shop has an empty name so that the first character of name field is '\0'.
Description
In this task, you are required to implement the following four functions.
The first function writes the product array into a binary file.
The function declaration is
int write_binary(const char* filename, const Product* shop);
where
filename
is the path of the binary file. For this task, it is the name of the file since the file is in the same directory as the executable.
shop
is the array of products in the shop. The last product has an empty name, i.e., its name has null termination character '\0' as the first character.
If the function succeeded, it should return 0, otherwise it should return 1.
The second function reads the shop products from the specified binary file.
The function declaration is
Product* read_binary(const char* filename);
where
filename
is the path of the binary file. For this task, it is the name of the file since the file is in the same directory as the executable.
The function returns a dynamically allocated array of products read from the file in the read order.
The last element of the array must have an empty name, i.e., its name has null termination character '\0' as the first character.
If an error occurs, it return NULL.
The third function writes the shop products into a text file.
The function declaration is
int write_plaintext(const char* filename, const Product* shop);
where
filename
is the path of the text file. For this task, it is the name of the file since the file is in the same directory as the executable.
shop
is the array of products in the shop. The last product has an empty name, i.e., its name has null termination character '\0' as the first character.
The file writes each product as follows.
In other words, the structure fields are separated with a space, and the array elements are separated by a newline. Because the data items are separated by a space, the product name should not have spaces in it.
The last array element (with the name of null character) should not be written into the file.
If the function succeeded, it should return 0, otherwise it should return 1.
An example file would have the content below.
yoghurt 1.223
muesli 4.312
The last function reads the shop products from the specified text file.
The function declaration is
Product* read_plaintext(const char* filename);
where
filename
is the path of the text file. For this task, it is the name of the file since the file is in the same directory as the executable.
The function returns a dynamically allocated array of products read from the file in the read order.
The last element of the array must have an empty name, i.e., its name has null termination character '\0' as the first character.
If an error occurs, it return NULL.
Important
Do not write the last array element into the files. However, you have to remember to add the last element with empty name yourself to the end of the array.
When dealing with plaintext files, do not add any format specifier modifier, i.e., read a floating point with %f, not with %0.1f.
Hint
When reading plaintext data, you can use sscanf function.
When dealing with binary files, you can use fread and fwrite functions for each Product type element.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!