LQ Newbie
Registered: May 2009
Posts: 5
Original Poster
Rep:
|
Code:
int loadData(FMSType* fms, char* fareFile, char* subfareFile)
{
CategoryTypePtr temp=NULL, startCategory=NULL, category=NULL, cur=NULL, prev=NULL;
ItemTypePtr tempItem=NULL, startItem=NULL, item=NULL, curItem=NULL, prevItem=NULL;
char line[LINE_LENGTH];
char subline[LINE_LENGTH];
char *catID;
char *catName;
char *catType;
char *catDesc;
char *itemID;
char *itemName;
char *itemDesc;
char *fareID;
char *token;
unsigned dollar,cent;
FILE *fp1,*fp2;
startCategory=(CategoryTypePtr )malloc(sizeof(CategoryType)); /* Dynamically creating category structure variable */
startCategory = NULL;
fp1=fopen(fareFile,"r"); /* opening fare.dat file in read mode */
fp2=fopen(subfareFile,"r");
if(fp1 == NULL || fp2 == NULL)
{
if(fp1 == NULL)
{
printf("%s does not exist!!\nPlease provide the correct file. \n",fareFile);
}
else if(fp2 == NULL)
{
printf("%s does not exist!!\nPlease provide the correct file.\n",subfareFile);
}
else if(fp1 == NULL && fp2 == NULL)
{
printf("%s and %s does not exist!!\n Please provide bothe files.\n",fareFile,subfareFile);
}
return SUCCESS;
}
else
{
while((fgets(line,LINE_LENGTH,fp1)) != NULL)
{
category=(CategoryTypePtr )malloc(sizeof(CategoryType));
catID=strtok(line,DELIM);
strcpy(category->categoryID,catID);
catType=strtok(NULL,DELIM);
strcpy(category->fareType,catType);
catName=strtok(NULL,DELIM);
strcpy(category->categoryName,catName);
catDesc=strtok(NULL,DELIM);
strcpy(category->categoryDescription,catDesc);
category->nextCategory=NULL;
category->headItem=NULL;
/* To create the linked list for the fare.dat file */
if(startCategory == NULL) /* To check whether the list already exists or not */
{
startCategory = category;
}
else /* If its not the first node created then do this else part */
{
temp = (CategoryTypePtr )malloc(sizeof(CategoryType));
temp = startCategory;
while(temp->nextCategory != NULL)
{
temp = temp->nextCategory;
}
temp->nextCategory = category;
}
} /* End of while(EOF) */
} /* End of else */
temp = startCategory;
if(temp == NULL)
{
printf("The list is empty\n");
}
else
{
printf("The list contents are:\n");
while(temp != NULL)
{
printf("Fare CategoryID: %s\nFare Type: %c\nFare Name: %s\nCategory Description: %s\n", temp->categoryID,temp->fareType,temp->categoryName,temp->categoryDescription);
temp= temp->nextCategory;
}
}
/* List for subfare.dat file */
while((fgets(subline,LINE_LENGTH, fp2)) != NULL)
{
item = (ItemTypePtr )malloc(sizeof(ItemType));
itemID=strtok(subline,DELIM);
strcpy(item->itemID,itemID);
fareID=strtok(NULL,DELIM);
itemName=strtok(NULL,DELIM);
strcpy(item->itemName,itemName);
token = strtok(NULL,".");
dollar = atoi(token);
token = strtok(NULL,"|");
cent = atoi(token);
item->prices[0].dollars = dollar;
item->prices[0].cents = cent;
itemDesc=strtok(NULL,DELIM);
strcpy(item->itemDescription,itemDesc);
item->nextItem=NULL;
temp=startCategory;
while(temp->nextCategory != NULL)
{
if(strcmp(fareID,temp->categoryID) == 0) /* Checking whether the fareIDs are same*/
{
if(temp->headItem == NULL)
{
temp->headItem=item;
}
else
{
tempItem=temp->headItem;
while(tempItem->nextItem != NULL)
{
tempItem=tempItem->nextItem;
}
tempItem->nextItem=item;
}
}
temp=temp->nextCategory;
}
printf("This is out of list loop\n");
} /* End of while(EOF) */
} /* End of else */
/*printf("The contents of the complete list are:\n");*/
/*temp=startCategory;*/
if(temp == NULL)
{
printf("The list is empty\n");
}
else
{
printf("The list contents are:\n");
while(temp != NULL)
{
printf("\nFare CategoryID: %s\nFare Type: %c\nFare Name: %s\nCategory Description: %s", temp->categoryID,temp->fareType,temp->categoryName,temp->categoryDescription);
tempItem=temp->headItem;
if(tempItem == NULL)
{
printf("The subfare list is empty for this fareID\n");
}
else
{
while(tempItem != NULL)
{
printf("ItemId: %s\nItem Name: %s\nDollar:%d\n Cents:%d\nItem description:%s",tempItem->itemID,tempItem->itemName,tempItem->prices[0].dollars,prices[0].cents,tempItem->itemDescription);
}
temp= temp->nextCategory;
}
/*temp=temp->nextCategory;*/
}
}
if(fclose(fp1)!=0 || fclose(fp2)!=0)
{
printf("Error in closing files\n");
}
return EXIT_SUCCESS;
}
|