my program is printing garbage now i know that my printData is wrong can any one suggest a way to fix itCode:/* This program expects the name of an input file and an output file to be given by the user. Name : Dang Vo Date : 04/14/11 */ #include <stdio.h> #include <stdlib.h> #include <string.h> #define NUM_LINE 21 typedef struct { char states[50]; char cities[50]; int tempt; } INFO; void getData(INFO list[]); void insertionSort(INFO list[], int last); //int calcAver(INFO list[]); void printData(INFO list[]); int main (void) { // Local Declarations //FILE *spData = fopen("in.txt", "r"); INFO list[NUM_LINE]; int last = 1; INFO states[50]; INFO cities[50]; INFO tempt; // Statements getData(list); insertionSort(list, last); calcAver(list); printData(list); return 0; } /*========================== getData======================== Pre: FILE *spData Post: FILE *spData */ // Local Declarations void getData(INFO list[]) { // Local declarations FILE *spData; char states[50]; char cities[50]; int tempt; //open file spData = fopen("in.txt", "r"); if (spData != NULL) { fgets(states, 50, spData); fgets(cities, 50, spData); scanf( "%d", &tempt ); printf("Sucessfully open in.txt\n"); fclose(spData); } else { printf("Could not open in.txt\n"); } return; } /*==============================insertionSort ================================ Pre: unsorted info Post: sorted info */ void insertionSort(INFO list[], int last) { // Local Declarations bool located; INFO temp; INFO *pCurrent; INFO *pWalker; INFO *pLast; // Statements for (pCurrent = list + 1, pLast = list + last; pCurrent <= pLast; pCurrent++) { located = false; temp = *pCurrent; for (pWalker = pCurrent - 1; pWalker >= list && !located;) if (strcmp(temp.states, pWalker->states) < 0) { *(pWalker + 1) = *pWalker; pWalker--; } else located = true; *(pWalker + 1) = temp; } return; } /*=============================== printData ================================ Pre: unsorted list POst:sorted list, average temperature */ void printData(INFO list[]) { FILE *spData; char states[50], cities[50]; int tempt; //open file spData = fopen("in.txt", "w"); if (spData != NULL) { fprintf(spData,"%s %s %d", states, cities, tempt); printf("Sucessfully open in.txt\n"); fclose(spData); } else { printf("Could not open in.txt\n"); } return; }
attachment under is the instruction and the file