I get a segfault not sure from what could someone help me debug it please?
Code:int loadData(GJCType* menu, char* menuFile, char* submenuFile) { /*pointer for malloc*/ GJCType *newP; /*declaration of variables*/ int CountNoLines = 0; char temp[LINE_LENGTH]; /* variables for tokenizer for menu file*/ char *catID; char *catType; char *catName; char *catDescription; /* declare file pointers to files*/ FILE *f1; FILE *f2; /* opening the menu and submenu for reading*/ f1 = fopen(menuFile, "r"); f2 = fopen(submenuFile, "r"); /* check to see if the file exists*/ if(f1 ==NU LL || f2 == NULL) { /* check whether both files have existed or typed in correctly*/ if(f1 == NULL && f2 == NULL) { printf("Both files do not exist %s %s\n",menuFile,submenuFile); } /* check for each file existance*/ else if (f1 == NULL) { printf("%s does not exist\n\n",menuFile); } /* check for each file existance*/ else if(f2 == NULL) { printf("%s does not exist\n\n", submenuFile); } printf("EXITING PROGRAM ERROR!\n\n"); return ERRORCODE; /* cannot proceed*/ } /* counts how many fields there are in the file*/ while(fgets(temp, LINE_LENGTH, f1)!=NULL) { /*temp[strlen(temp)]= '\0'; printf("%s", temp);*/ if(!countToken(f1, temp, TOKEN_PRODUCT)) { return ERRORCODE; } CountNoLines++; } printf("%d", CountNoLines); /* set f1 pointer to the start of the file for reading*/ fseek(f1, 0, SEEK_SET); /* if CountNoLines is 0 do nothing*/ if(CountNoLines == 0) { ; } /* if there is more than 0 lines tokenize fields check if there is duplication and check the string length and if its too big its the wrong data format */ if(CountNoLines > 0) { while(fgets(temp, LINE_LENGTH, f1) !=NULL) { /* stores the catID but also checks if there is a duplicate*/ catID = strtok(temp, "|"); /*checks for a duplication of the id field*/ if(!checkDuplicationID(menu, catID)) { return ERRORCODE; } /* gets the second field and tokenizez it*/ catType = strtok(NULL, "|"); catName = strtok(NULL, "|"); catDescription = strtok(NULL, "\0"); /* checks the string length of the field*/ if((strlen(catID) >ID_LEN) || (strlen(catType) >MAX_NAME_LEN) || (strlen(catDescription) >MAX_DESC_LEN)) { printf("Wrong data format\n\n"); return ERRORCODE; } /* malloc the pointers*/ newP = malloc(sizeof (GJCType)); newP->headCategory = malloc(sizeof(CategoryType)); newP->headCategory->headItem = malloc(sizeof(ItemType)); newP->headCategory->nextCategory = malloc(sizeof(CategoryType)); newP->headCategory->headItem->nextItem =malloc(sizeof(ItemType)); /* check if it cannot allocate memory*/ if(newP == NULL) { printf("Memory could not be allocated\n\n"); return ERRORCODE; } /*copy variables into structure*/ strcpy(newP->headCategory->categoryID, catID); newP->headCategory->drinkType = *catType; /*printf("%c", newP->headCategory->drinkType);*/ /* strcpy(newP->headCategory->drinkType, catType);*/ strcpy(newP->headCategory->categoryName, catName); strcpy(newP->headCategory->categoryDescription, catDescription); newP->headCategory->nextCategory = NULL; } } /* close both files after reading*/ if(fclose(f1)!=0 || fclose(f2)!=0) { fprintf(stderr, "Error in closing files\n"); } return EXIT_SUCCESS; }



LinkBack URL
About LinkBacks


