# Segmentation Fault! Plz Help

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

Not open for further replies.

Joined:
Sep 23, 2008
Messages:
3
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
{
unsigned categoryCount;
DenomType cashTypes[NUMDENOMS];
}fttType;

typedef fttType * fttTypePtr;

#include "ftt_options.h"
#include "ftt_utility.h"
#endif

BOOLEAN LoadData(fttTypePtr ftt, char* categoryFile, char* itemFile,
char* moneyFile)
{
char input[80];
char ch;
char *categoryInput=NULL;
printf("Unable to open file.");
}
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];

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)
{
}
/* 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*/

fclose(fp1);

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;

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*/

/* 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++;
}

if(fclose(fp2)!=0)
{
fprintf(stderr, "cannot close file\n");
}

return FALSE;
}

```