Question: I cannot figure out why I get a segmentation fault when I try to print my data. This should read in lines from a file

I cannot figure out why I get a segmentation fault when I try to print my data. This should read in lines from a file that are all formatted: string float int string. Ex. "word 2.5 4 text". open the file, count the lines, dynamically create the memory for the structs to hold the data. sort the structs either by int or float value, then print the structs in ascending or descending order. Help please.

//Include statements #include  #include  #include  //create struct to store each line of data struct dataLine { int lineInt; float lineFloat; char string1[50]; char string2[50]; }temp, *ptemp; //Function prototypes int detSize(char *filename); void loadFile (char *filename, int size, struct dataLine **lineList); int printMenu(); void menuTree(int selection, int size, struct dataLine **lineList); void sortFloat(int size, struct dataLine **lineList); void sortInt(int size, struct dataLine **lineList); void printHtoL(int size, struct dataLine **lineList); void printLtoH(int size, struct dataLine **lineList); //functions int detSize(char *filename) { int size = 0; FILE *file; file = fopen(filename, "r"); while (1) { fscanf(file, "%s", temp.string1); fscanf(file, "%f", &temp.lineFloat); fscanf(file, "%d", &temp.lineInt); fscanf(file, "%s", temp.string2); if (feof(file)) break; size++; } fclose(file); return size; } void loadFile (char *filename, int size, struct dataLine **lineList) { int i, y, f; lineList = (struct dataLine **)calloc(size, sizeof(*lineList)); for(y = 0; y < size; y++) { lineList[y] = (struct dataLine *)calloc(1, sizeof(struct dataLine)); } FILE *file; file = fopen(filename, "r"); for (i = 0; i < size; i++) { fscanf(file, "%s", (*lineList[i]).string1); fscanf(file, "%f", &(*lineList[i]).lineFloat); fscanf(file, "%d", &(*lineList[i]).lineInt); fscanf(file, "%s", (*lineList[i]).string2); if (feof(file)) break; } fclose(file); return; } int printMenu(){ int menuSelect; printf("Please select from the following options (1-5): "); printf("1. Sort data by the float value & print high to low "); printf("2. Sort data by the float value & print low to high "); printf("3. Sort data by the int value & print high to low "); printf("4. Sort data by the int value & print low to high "); printf("5. Exit "); scanf("%d", &menuSelect); return menuSelect; } void menuTree(int selection, int size, struct dataLine **lineList){ switch(selection){ case 1: sortFloat(size, lineList); printHtoL(size, lineList); case 2: sortFloat(size, lineList); printLtoH(size, lineList); case 3: sortInt(size, lineList); printHtoL(size, lineList); case 4: sortInt(size, lineList); printLtoH(size, lineList); case 5: return; default: printf("You did not give a valid input, please try again"); } return; } void sortFloat(int size, struct dataLine **lineList){ int swapped, i; printf("got to sortFloat"); do { swapped = 0; for (i = 0; i < size-1; i++){ if ((*lineList[i]).lineFloat > (*lineList[i+1]).lineFloat){ temp = *lineList[i]; *lineList[i] = *lineList[i+1]; *lineList[i+1] = temp; swapped = 1; } } } while (swapped); printf("finished sortFloat"); return; } void sortInt(int size, struct dataLine **lineList){ int swapped, i; do { swapped = 0; for (i = 0; i < size-1; i++){ if ((*lineList[i]).lineInt > (*lineList[i+1]).lineInt){ ptemp = lineList[i]; lineList[i] = lineList[i+1]; lineList[i+1] = ptemp; swapped = 1; } } } while (swapped); return; } void printHtoL(int size, struct dataLine **lineList){ int i; for (i = size-1; i >= 0; i--) { printf("%s ", (*lineList[i]).string1); printf("%f ", (*lineList[i]).lineFloat); printf("%d ", (*lineList[i]).lineInt); printf("%s ", (*lineList[i]).string2); } printf(" "); return; } void printLtoH(int size, struct dataLine **lineList){ int i; for (i = 0; i < size; i++) { printf("%s ", (*lineList[i]).string1); printf("%f ", (*lineList[i]).lineFloat); printf("%d ", (*lineList[i]).lineInt); printf("%s ", (*lineList[i]).string2); } printf(" "); return; } //main routine int main(void){ int size; struct dataLine **lineList; size = detSize("./hw4.data"); loadFile("./hw4.data", size, lineList); int menuSelect = 0; do { menuSelect = printMenu(); printf(" "); menuTree(menuSelect, size, lineList); } while (menuSelect != 5); //debugging data printf("made it to printing, %d lines ", size); int i; for (i = 0; i < size; i++) { printf("line %d: ", (i+1)); printf("%s ", (*lineList[i]).string1); printf("%f ", (*lineList[i]).lineFloat); printf("%d ", (*lineList[i]).lineInt); printf("%s ", (*lineList[i]).string2); } free(lineList); return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!