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.

Thread Status:
Not open for further replies.
  1. Adi_2021

    Adi_2021 New Member

    Joined:
    Sep 23, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hey guys.I need to load everything from the files into the respective linked lists. The categoryType linked lists seems to be working but i get a segmentation fault for itemContainerType...along with the code is the items.dat file from which i need to read and put stuff into....please see if u can help me out....cheers

    ITEMS.DAT

    I0001|Baclava|Rich Middle Eastern Cake of Thin Layers of flaky pastry filled with nuts and honey|800|C0002|C0003|C0001
    I0002|Pavlova|a meringue dessert named after the Russian ballet dancer|1200|C0003


    Code:
    
    #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;
    }MoneyType;
    
    /* 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;
    }DenomType;
    
    /* 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];
    }fttType;
    
    typedef fttType * fttTypePtr;
    
    #include "ftt_options.h"
    #include "ftt_utility.h"
    #endif
    
    
    
    
    
    /*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) {
    		categoryInput=strtok(input,"|");
    		while(categoryInput!=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);
          exit(0);
          
       }
       
       /* 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");
    	  exit(0);
          }
          
          /* 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*/
          else
          {
             prevCat->next = newCat;
          }
           
          
          /* tokenize the Pipe and copy the field
          pointers into the struct*/
               
          token = strtok(line, "|");
            if(token == NULL)
    	   {
    	      printf("CatID missing\n");
    	   }
    	   else
    	        strcpy(newCat->id, token);
               
          /*token = strtok(NULL, "|");   
          
          newCat->drinkType = token[0];*/
          
          token = strtok(NULL, "|");
          if(token == NULL)
    	   {
    	      printf("Cat Name missing\n");
    	   }
    	   else
          strcpy(newCat->name, token);
            
          token = strtok(NULL, "|");
          if(token == NULL)
    	   {
    	      printf("Cat desc missing\n");
    	   }
    	   else
          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;
       
        
      
    	fclose(fp1);
       
       
    
     
    
    
    
    
    /* 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);
          exit(0);
        }
       /*intialize pointer to the start*/
       prevItem = NULL;
       currentCat = ftt->categoryHead;
       
       while((line = fgets(array, 800, fp2)) != NULL)
       {
          newItem = malloc(sizeof(ItemContainerType));
        	
     	new=malloc(sizeof(ItemType));
    	
    	newItem->item=new;
    	
    	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);
    	d=(unsigned)j;
    	 
    	newItem->item->price.dollars=d/100;printf("%u\n",newItem->item->price.dollars);
    	newItem->item->price.cents=d%100;printf("%u\n",newItem->item->price.cents);
    		
    	
    		while(token!=NULL) 
                { 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->firstItem=newItem;
    			   	 break;
    			 }
    			 currentCat = currentCat->next;printf("outside if\n");
    		}
    	       printf("outside while\n");
    	       
            }
          
                /* from the start of the list*/
                newItem->next = currentCat->firstItem;
    	            currentCat->firstItem = newItem;
    	/*SEGMENTATION FAULT HERE*/    
    	    /* increment the counter*/
    	    ftt->categoryCount++;
    	 }
          
       
      currentCat = ftt->categoryHead;
       
       
     if(fclose(fp2)!=0)
       {
           fprintf(stderr, "cannot close file\n");
       }
       	
    
       return FALSE;
    }
    
    
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
Thread Status:
Not open for further replies.

Share This Page