Question: Debug the following program and make sure it accurately runs with any text input file: /*------------------------------------------------------------------ File: analyseFileData.c Author: Gilbert Arbez, Fall 2016 Description: Computes
Debug the following program and make sure it accurately runs with any text input file:
/*------------------------------------------------------------------ File: analyseFileData.c Author: Gilbert Arbez, Fall 2016 Description: Computes the average, minimum and maximum values of the data found in a file. ---------------------------------------------------------------------*/ #include
#define TXT_INPUT_FILE "data.txt" #define NUM 3 // number of computed values // The following are indexes into the array // containing computed values #define MINIX 0 #define MAXIX 1 #define AVGIX 2 // For creating bin file #define MAX_NUM_VALUES 500 // up to 500 values are saved in the bin file #define BIN_FILE "data1.bin" // function prototypes void analyseFile(FILE *, double *); void saveInBinFile(FILE *); /*--------------------------------------------------------------------- Function: main Description: Main opens a text file and calls analyseFile to determine the minimum, the maximum, and the average of all values in the file. Results are displayed. Finally calls saveInBinFile to save the contents of the file into a binary file (the binary file is used in the next exercise). ------------------------------------------------------------------------*/ int main() { // Variable declarations FILE* ifp; //input file pointer double outputVals[NUM]; // Open the input file ifp = fopen(TXT_INPUT_FILE, "r"); if(ifp == NULL) printf("Cannot open input file %s ",TXT_INPUT_FILE); else { // Analyze contents analyseFile(ifp, outputVals); // Display the results printf("Analysis of file data gives: "); printf(" Minimum: %.3f ", outputVals[MINIX]); printf(" Maximum: %.3f ", outputVals[MAXIX]); printf(" Average: %.3f ", outputVals[AVGIX]); saveInBinFile(ifp); fclose(ifp); } } /*----------------------------------------------------------------------- Function: analyseFile Parameters: fp - file pointer of file containing the data outPt - reference to array for storing output values Description: Scans all values in the file and computes three output values: the minimum value, the maximum value and the average value of the data in the file. ------------------------------------------------------------------------*/ void analyseFile(FILE *fp, double *outPt) { // Variable declarations double min, max; // minimum and maximum values of the data double avg; // average value int count; // To count number of values for computing average double val; // value read from the file // Start at the beginning of the file rewind(fp); // this is a safety check and allows this function // to be called multiple times with the same file. // Initialization of variables count = 0; fscanf(fp,"%lf",&val); // Read the first value min = val; max = val; // initialize min/max avg = 0.0; // Initialize to 0 for summing values while(feof(fp) == 0) // Not end of file { // Minimum value if(min > val) min = val; if(max < val) max = val; avg = avg + val; // sum for computing average count = count + 1; // need count to average. // get next value fscanf(fp,"%lf",&val); } // compute the average avg = avg/count; // save output in array outPt[MINIX] = min; outPt[MINIX] = max; outPt[AVGIX] = avg; }
/*----------------------------------------------------------------------- Function: saveInBinFile Parameters: fp - file pointer of input file containing the data in text form Description: Copies all data in the txt file into an array and then saves the data into a bin file. ------------------------------------------------------------------------*/ void saveInBinFile(FILE *fp) { // Declarations double values[MAX_NUM_VALUES]; // Cannot read more than MAX_NUM_VALUES int ix; // Indexes into array and counts number of values read FILE *binfp; // File pointer for binary file // Read in values into the array rewind(fp); // start at the beginning of the file for(ix = 0; ix < MAX_NUM_VALUES && feof(fp) == 0; ix = ix +1) fscanf(fp,"%lf", &values[ix]); // Note that after the loop ix equals the number of values read from the file // Open a binary file binfp = fopen(BIN_FILE, "wb"); if(binfp == NULL) printf("Cannot open binary file %s ", BIN_FILE); else { // Write array to binary file fwrite(values,sizeof(double),ix,binfp); fclose(binfp); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
