Question: c++ This lab is to write some code using C++ so that records stored in a binary file can be retrieved for processing. Each record
c++
This lab is to write some code using C++ so that records stored in a binary file can be retrieved for processing. Each record occupies the same amount of space in the file.
A template has been provided to you for completion.
The template has provided the following contents.
Data Structure Definition
A data structure named NutritionData that contains these fields
foodName (array of 40 char) , use an all uppercase integer constant identifier for the size.
servingSize (double)
calFromCarb (double)
calFromFat (double)
calFromProtein (double)
Function Prototypes
- The prototype of the function that retrieve certain NutritionData record from a binary file.
- The prototype of the function that prints a NutritionData record.
Main Function
You need to complete the main function as follows:
- call the function that can retrieve a nutrition data record to retrieve the third record from the file "nutri.dat".
- and save it to the location where it can be read by your executable directly.
- If the call was successful, call the print function to display the retrieved record. Otherwise, print "The desired structure cannot be extracted.
Definition of the function that can retrieve a nutrition data.
The function has the following parameters:
A parameter for the file name. This is a const string reference parameter.
A parameter for returning the retrieved NutritionData.
A parameter for the desired structure. It must be a positive integer. If the value is 1, the function will attempt to return the first structure on file. If the value is 2, the function will attempt to return the second structure on file. And so on.
The function returns true if the desired record has been successfully retrieved from the file, otherwise it returns false.
You need to complete the body of the function as follows.
- Open the file as an input binary file using the file name parameter.
- If the file doesn't exist, print the message, "The file filename cannot be opened." Replace the word filename with the value of the file name parameter.
- If the file exists, access the desired record from the file. The program should use the seek mechanism to access the desired record. Note that the first record is at offset 0, the second record is at the offset that equals the size of a record, etc.
- if the seek is successful, read the desired record and store that in the caller's NutritionData variable through the pointer parameter.
Close the file.
Note that the function should return true only if the file has been opened successfully and the desired record has been retrieved and stored in the parameter. Otherwise, it should return false. Therefore, you need to have code that tests the status of the file for success or error at each file operation.
You need to complete the print function to print a NutritionData information in the expected format.
Test The Program
The output should look exactly as follows:
Food Name: Bread pita whole wheat
Serving Size: 64.0 grams
Calories Per Serving: 170.6
Calories From Carb: 134.0
Calories From Fat: 14.0
Calories From Protein: 22.6

#include
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
