I am trying to read a specific record from a file.
The "nutri.dat" was written by using a class that iterated to write the user input. Now I want to read the third iteration and print it on the screen but it's not executing like I want it to. Please help.
int main() {
string filename = "nutri.dat";
NutritionData *pointer = new NutritionData;
int record{4};
if(retrieveNutritionData(filename, pointer, record)!= 0){
printNutritionData(pointer);
}
else
cout << "The desired structure cannot be extracted." << endl;
return 0;
}
//***********************************************************************
//* Definition of the function that can retrieve a nutrition data.
//*
//* The function has the following parameters:
//*
//* fileName - a const string reference parameter for the file name.
//*
//* dataPtr - a pointer to the callers NutrititionData variable
//* for returning the retrieved NutritionData.
//* recordNum - a parameter for the desired structure.
//*
//* The parameter recordNum 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.
//*
//***********************************************************************
//***********************************************************************
//* Definition of the function that can print a nutrition data.
//* The function calculates the total calories per serving by
//* adding the calories from three nutrition components.
//*
//* The function has the following parameter.
//*
//* dataPtr - a pointer to the callers NutrititionData variable.
//*
//***********************************************************************
void printNutritionData(const NutritionData *dataPtr) {
cout << dataPtr->foodName << endl;
cout << dataPtr->servingSize << endl;
cout << dataPtr->calFromCarb << endl;
cout << dataPtr-> calFromFat << endl;
cout << dataPtr-> calFromProtein << endl;
And the input.read() statements are reading the size-of-a-pointer worth of data (sizeof(dataPtr)) when the size needs to be that of the relevant struct member.
Andy
PS As you're reading structs member by member, it might be worthwhile reading up about struct packing?