1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Segmentation Fault! Plz Help

Discussion in 'C' started by Adi_2021, Sep 28, 2008.

  1. Adi_2021

    Adi_2021 New Member

    Sep 23, 2008
    Likes Received:
    Trophy Points:
    Hey guys...some one was working on a similiar program earlier...anyway...this is my function load data....i have put a comment to show where i am getting my segmentation fault....i have two structures, involved in this function, CategoryType and ItemContainerType. I am able to lad the data from category file but have a problem with Item ContainerType.

    #ifndef FTT_H
    #define FTT_H
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define IDLEN 8
    #define NAMELEN 20
    #define DESCLEN 256
    #define EXTRACHARS 2
    #define SEPARATOR "|"
    /* the different denominations of money in existence there are 11 - 
     * 5c, 10c, 20c, 50c, $1, $2, $5, $10, $20, $50, $100
    #define NUMDENOMS 11 
    /*define a boolean datatype where FALSE is 0 and TRUE is non-zero 
    typedef enum truefalse
      FALSE=0, TRUE
    } BOOLEAN;
    /* money type is used in the system to represent dollars and cents without
     * using a floatign point type such as float or double
    typedef struct money
      unsigned dollars;
      unsigned cents;
    /* denomtype is used in the system to represent a particular denomination
     * of money. the value is how much a single item in this denomination would
     * be worth (5 for 5 cents, 10 for 10 cents, etc) 
    typedef struct denom
      unsigned value;
      unsigned count;
    /* ItemType contains the base information about each item on the menu.
     * It only contains the information for one item and has nothing about 
     * which menu category it will be on. This is because we want to allow
     * a single item to be on more than one menu category
    typedef struct itemType
      char id[IDLEN + EXTRACHARS];
      char name[NAMELEN + EXTRACHARS];
      char desc[DESCLEN + EXTRACHARS];
      MoneyType price;							
    } ItemType;
    typedef ItemType * ItemPtr;
    /* ItemContainerType is the 'holder' for each Item in memory. We have used
     * this so that one item can be listed under multiple categories as this 
     * was one of Fred's requirements for his menu system
    typedef struct itemContainer
      ItemPtr item;
      struct itemContainer * next;
    } ItemContainerType;
    typedef ItemContainerType * ItemContainerPtr; 
    /* CategoryType is the list type which is at the top of the list hierarchy.
    typedef struct catType
      char id[IDLEN + EXTRACHARS];
      char name[NAMELEN + EXTRACHARS];
      char desc[DESCLEN + EXTRACHARS];
      ItemContainerPtr firstItem;
      struct catType * next;
    } CategoryType;
    typedef CategoryType* CategoryPtr;
    /* fttType is the datatype that you will be passing around the system as 
     * the main datatype parameter. It is the link into the rest of the 
     * list hierarchy.
    typedef struct ftt
      CategoryPtr categoryHead;
      unsigned categoryCount;
      DenomType cashTypes[NUMDENOMS];
    typedef fttType * fttTypePtr;
    #include "ftt_options.h"
    /*LOAD DATA*/
    BOOLEAN LoadData(fttTypePtr ftt, char* categoryFile, char* itemFile,
      char* moneyFile)
    /*	FILE *readfile;
    	char input[80];
    	char ch;
    	char *categoryInput=NULL;
    	readfile = fopen(categoryFile, "r");
    	if (readfile == NULL) {
    		printf("Unable to open file."); 
    		while(fgets(input, 80, readfile)!=NULL) {
    		printf( "%s\n", categoryInput );
        		categoryInput = strtok( NULL, "|" );
        CategoryType *currentCat, *prevCat, *newCat;
       ItemContainerType *currentItem, *prevItem, *newItem;
    	ItemType *current, *prev, * new;
    /*MoneyType *newmoney;*/
       FILE *fp1;
       FILE *fp2;
       char *token, *line, *token2, *line2;
       char array[800];
       char array2[800];
       unsigned d;
       double temp;
       int i,j;
       char nc_array[500];
       /* Open menu file for reading. */
       fp1 = fopen(categoryFile, "r");
       /* check if fp1 menu file exists*/
       if(fp1 == NULL)
          printf("file %s does not exist \n", categoryFile);
       /* initialize the previous node to NULL*/
       prevCat = NULL;
       while((line = fgets(array, 800, fp1)) != NULL)
          /* allocate memory for CategoryType pointer*/
          newCat = malloc(sizeof(CategoryType));
          /* check if memory allocation succeeded if fails exit*/
          if(newCat == NULL)
              printf("Memory Allocation error for newCat\n");
          /* if the prevCat is NULL point the new node to the 
          start of the list*/
          if(prevCat == NULL)
             ftt->categoryHead = newCat;
          /* if it isnt at the start get the next -1node*/
             prevCat->next = newCat;
          /* tokenize the Pipe and copy the field
          pointers into the struct*/
          token = strtok(line, "|");
            if(token == NULL)
    	      printf("CatID missing\n");
    	        strcpy(newCat->id, token);
          /*token = strtok(NULL, "|");   
          newCat->drinkType = token[0];*/
          token = strtok(NULL, "|");
          if(token == NULL)
    	      printf("Cat Name missing\n");
          strcpy(newCat->name, token);
          token = strtok(NULL, "|");
          if(token == NULL)
    	      printf("Cat desc missing\n");
          strcpy(newCat->desc, token);
          /* initialize everything to a safe state*/
          newCat->next = NULL;
          newCat->firstItem= NULL;
          prevCat = newCat;
       /* the current pointer points to headCategory*/
       currentCat = ftt->categoryHead;
       /* for testing purposes traverse through the list
       and print out linked list*/
       /*printf("Checking to see if updated\n");
       while(currentCat != NULL)
          printf("\n%s, %s, %s", currentCat->id, 
          /* get the next row*/
         /* currentCat = currentCat->next;
       /* close the first pointer to the file*/
       /* check if it can close it otherwise error*/
    	/*   if(fclose(fp1)!=0)
           fprintf(stderr, "cannot close file\n");
    /* Open submenu file for reading. */
       fp2 = fopen(itemFile, "r");
       /* check if sub menu file exists*/
       if(fp2 == NULL)
          printf("file %s does not exist \n", itemFile);
       /*intialize pointer to the start*/
       prevItem = NULL;
       currentCat = ftt->categoryHead;
       while((line = fgets(array, 800, fp2)) != NULL)
          newItem = malloc(sizeof(ItemContainerType));
    	token = strtok(line, "|");
           strcpy(newItem->item->id, token);
           token = strtok(NULL, "|");
          strcpy(newItem->item->name, token);
           token = strtok(NULL, "|");
          strcpy(newItem->item->desc, token);
    	 token = strtok(NULL, "|");
    	j = atoi(token);
                { token = strtok(NULL, "|");
    	      strcpy(nc_array, token);
    	      /* put the current pointer to the head of the list*/
    	      currentCat = ftt->categoryHead;
    	      /* while current is pointing to a node*/
    	      while(currentCat != NULL)
    	      {	printf("Current Cat Id:%s\n",currentCat->id );
    			printf("Currrent nc_array:%s\n",nc_array);
    			 if(strcmp(currentCat->id, nc_array) == 0)
    			 {	printf("inside if\n");
    			 currentCat = currentCat->next;printf("outside if\n");
    	       printf("outside while\n");
                /* from the start of the list*/
                newItem->next = currentCat->firstItem;
    		printf("Lookin at seg fault2 \n");
                currentCat->firstItem = newItem;
    	    /* increment the counter*/
      currentCat = ftt->categoryHead;
       /* try to close the file*/
       /* if it wont close provide an error*/
           fprintf(stderr, "cannot close file\n");
    return FALSE;

Share This Page