(C Programming) Use The Attached Code and add in code that makes the program sav
ID: 3698758 • Letter: #
Question
(C Programming) Use The Attached Code and add in code that makes the program save and load from a binary file. Do not forget to free up memory!
#include <stdio.h>
#include <stdlib.h>
#define PAUSE system("pause")
typedef struct {
int age;
int weight;
int height;
} STATS;
STATS *makeArrayOfPointers();
void loadArray(STATS **array, int *c);
void displayArray(STATS **array, int c);
main(){
STATS** array;
int count = 0;
int size = 500;
array = makeArrayOfPointers(size);
printf("the address of the array is %p ", array);
PAUSE;
loadArray(array, &count);
displayArray(array, count);
printf("Age is %i. ", array[0]->age);
PAUSE;
} // end of main
void displayArray(STATS **array, int c){
int i;
for(i = 0; i < c; i++) {
printf("Record[%i] Age is %i. Weight is %i. Height is %i. ", i, array[i]->age, array[i]->weight, array[i]->height);
}
PAUSE;
} // end displayArray
void loadArray(STATS **array, int *c){
int value;
int counter = *c;
for(*c; *c < (counter + 4); *c = *c + 1){
array[*c] = calloc(1, sizeof(STATS));
printf("Enter age: ");
scanf("%i", &value);
array[*c]->age = value;
printf("Enter weight: ");
scanf("%i", &value);
array[*c]->weight = value;
printf("Enter height: ");
scanf("%i", &value);
array[*c]->height = value;
} // end for
} // end loadArray
STATS *makeArrayOfPointers(int size) {
STATS *result;
result = malloc(sizeof(STATS*) * size);
return result;
} // end makeArrayOfPointers
Explanation / Answer
Please find my implementation:
Please change the path of binary file according to your file location
Please rate my answer if it helped you!!
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define PAUSE system("pause")
typedef struct {
int age;
int weight;
int height;
} STATS;
STATS *makeArrayOfPointers(int );
void loadArray(STATS **array, int *c);
void displayArray(STATS **array, int c);
void saveArray(STATS **array, int count, int size);
STATS **reloadArray(char *, int *count, int *size);
main() {
STATS** array = 0;
int count = 0;
int size = 500;
char reloaded = 'N';
array = reloadArray(&reloaded, &count, &size);
if(reloaded == 'N')
array = makeArrayOfPointers(size);
PAUSE;
loadArray(array, &count);
printf("** unSorted Array ** ");
displayArray(array, count);
//sortArray(array, count)
printf("** Sorted by Age ** ");
displayArray(array, count);
saveArray(array, count, size);
PAUSE;
} // end of main
void displayArray(STATS **array, int c) {
int i;
for (i = 0; i < c; i++) {
printf("Record[%i] Age is %i. Weight is %i. Height is %i. ", i, array[i]->age, array[i]->weight, array[i]->height);
}
PAUSE;
} // end displayArray
void loadArray(STATS **array, int *c) {
int value;
int counter = *c;
for (*c; *c < (counter + 4); *c = *c + 1) {
printf(" Information for person: %i ", (*c) + 1);
array[*c] = calloc(1, sizeof(STATS));
printf("Enter age: ");
scanf("%i", &value);
array[*c]->age = value;
printf("Enter weight: ");
scanf("%i", &value);
array[*c]->weight = value;
printf("Enter height: ");
scanf("%i", &value);
array[*c]->height = value;
} // end for
} // end loadArray
STATS *makeArrayOfPointers(int size) {
STATS *result;
result = malloc(sizeof(STATS*) * size);
return result;
} // end makeArrayOfPointers
void saveArray(STATS **array, int count, int size) {
FILE *ptr;
int i;
ptr = fopen("c:\myBinFile.bin", "wb"); // from binary file
if (ptr == NULL) {
printf("Could not open the file ");
PAUSE;
exit(-1);
}
// SAVE THE SIZE OF THE ARRAY
fwrite(&size, sizeof(int), 1, ptr);
// SAVE THE EFFECTIVE SIZE or COUNT
fwrite(&count, sizeof(int), 1, ptr);
// SAVE EACH NODE/ELEMENT in the ARRAY
for (i = 0; i < count; i++) {
fwrite(array[i], sizeof(STATS), 1, ptr);
} // end for
fclose(ptr);
}// end saveArray
STATS **reloadArray(char *result, int *count, int *size) {
STATS **temp = 0;
FILE *ptr;
int i;
*result = 'Y';
ptr = fopen("c:\myBinFile.bin", "rb"); // binary file
if (ptr == NULL) {
printf("Could not open the file ");
PAUSE;
*result = 'N';
}
else {
// Reload the size of the array
fread(size, sizeof(int), 1, ptr);
// Create the Array of Pointers
temp = makeArrayOfPointers(*size);
// Reload the count or effective size variable
fread(count, sizeof(int), 1, ptr);
// Reload the nodes or elements of the array
for (i = 0; i < *count; i++) {
temp[i] = calloc(1, sizeof(STATS));
fread(temp[i], sizeof(STATS), 1, ptr);
}
} // end else
fclose(ptr);
return temp;
}// end reloadArray
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.